jqueryajax自定义头
我正在尝试执行对iContact API的请求,该请求要求我使用自定义头进行身份验证(http://developer.icontact.com/documentation/authenticate-requests). 这是我的代码:jqueryajax自定义头,jquery,ajax,google-chrome,Jquery,Ajax,Google Chrome,我正在尝试执行对iContact API的请求,该请求要求我使用自定义头进行身份验证(http://developer.icontact.com/documentation/authenticate-requests). 这是我的代码: $.ajax({ type: "GET", url: "https://app.icontact.com/icp/a/", contentType: "application/json", bef
$.ajax({
type: "GET",
url: "https://app.icontact.com/icp/a/",
contentType: "application/json",
beforeSend: function(jqXHR, settings){
jqXHR.setRequestHeader("Accept", "application/json");
jqXHR.setRequestHeader("Api-Version", iContact_API_version);
jqXHR.setRequestHeader("Api-AppId", iContact_appID);
jqXHR.setRequestHeader("Api-Username", iContact_username);
jqXHR.setRequestHeader("API-Password", iContact_appPassword);}
});
由于某种原因,请求没有通过。然而,当我手动执行相同的请求时(使用Chrome REST控制台),它工作得很好。如果我取出自定义标题(API-*),请求将通过,但当然身份验证失败,我将返回一个常规HTML页面
我切换到Firefox并检查了请求/响应标题:
请求:
Host app.icontact.com
User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Origin http://184.72.61.244
Access-Control-Request-Me... GET
Access-Control-Request-He... api-appid,api-password,api-username,api-version
答复:
HTTP/1.1 302 Found
Date: Tue, 14 Jun 2011 23:43:56 GMT
Server: Apache/2.2.9 (Debian)
Set-Cookie: intellicontact_phpsess=1c7ca333017b47f46edd893dae584781; path=/; domain=.icontact.com; secure; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: https://app.icontact.com/icp/login/sentry.php?relurl=https%3A%2F%2Fapp.icontact.com%2Ficp%2Fa%2F&sess=
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Connection: close
Content-Type: text/html; charset=utf-8
知道这里出了什么问题吗
谢谢 奇怪的是,您的响应中出现了302(重定向)。您是否尝试过使用Ajax函数记录错误以解决问题?这是我用过的一个例子
console.log(" call back url :"+ callBackURL);
$.ajax({
url: callBackURL,
type: 'post', //'GET' in your case
contentType: 'application/json; charset=utf-8',
dataType: 'json' , // explicitly mentioning datatype
success: function(json) {
console.log(" reponse :"+ json);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("error :"+XMLHttpRequest.responseText);
}
});
请试试这个,你一定能解决问题
function FormSubmit()
{
$.ajax({
type: "POST",
url: 'index.php',
data: $("#form_main").serialize(),
async: false }).done(function( data ) {
$("#Response").hide();
if (isNaN(data)) {
dispmsg = "<div class='alert alert-danger' role='alert'>Oops..Something had gone wrong! Please try again!</div>";
$("#assess_me_response").html(dispmsg); setTimeout('ResetForm();',3000);
}
else {
dispmsg = "<div class='alert alert-success' role='alert'>Thank you for sharing the details. Our Consultant will contact you shortly!</div>"; $("#assess_me_response").html(dispmsg); setTimeout('Redirect();',3000);
}
return true;
});
}
函数FormSubmit()
{
$.ajax({
类型:“POST”,
url:'index.php',
数据:$(“#form_main”).serialize(),
async:false}).done(函数(数据){
$(“#响应”).hide();
if(isNaN(数据)){
dispmsg=“哎呀……出了问题!请再试一次!”;
$(“#评估我的响应”).html(dispmsg);setTimeout('ResetForm();',3000);
}
否则{
dispmsg=“感谢您分享详细信息。我们的顾问将很快与您联系!”$(“#评估我的响应”).html(dispmsg);setTimeout('Redirect();',3000);
}
返回true;
});
}
beforeSend钩子只对$.ajaxSetup()
可用,因此如果只想将其添加到$.ajax()
中,只需使用headers
属性即可
如果要向单个请求添加自定义标头(或一组标头),只需添加headers属性:
// Request with custom header
$.ajax({
url: 'foo/bar',
headers: { 'x-my-custom-header': 'some value' }
});
如果要向每个请求添加默认头(或一组头),请使用$.ajaxSetup()
:
如果要向每个请求添加一个头(或一组头),请使用发送前的钩子$。ajaxSetup()
:
编辑(更多信息):需要注意的一点是,使用ajaxSetup
只能定义一组默认标题,在发送之前只能定义一个。如果多次调用ajaxSetup
,则只会发送最后一组标头,并且只会执行发送前的最后一次回调
来源:您可能正在查看一个选项请求,即查看目标主机是否允许跨域请求。解决方法是使用JSONP,但它不支持自定义头。如果iContact不支持跨域ajax请求或JSONP,您可能会使用或诸如此类的内容。通常,如果答案中包含对代码意图的解释,以及在不引入其他内容的情况下解决问题的原因,那么答案会更有帮助。添加“try this”不起作用。jQuery.ajaxSetup()的官方文档声明不建议使用它:“为将来的Ajax请求设置默认值。\uuu不建议使用它。”
$.ajaxSetup({
headers: { 'x-my-custom-header': 'some value' }
});
// Sends your custom header
$.ajax({ url: 'foo/bar' });
// Overwrites the default header with a new header
$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } });
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('x-my-custom-header', 'some value');
}
});
// Sends your custom header
$.ajax({ url: 'foo/bar' });
// Sends both custom headers
$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } });