微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jQuery AJAX自定义标题

我正在尝试对iContact API执行一个请求,这需要我使用自定义标头进行身份验证(http://developer.icontact.com/documentation/authenticate-requests).这是我的代码
$.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

任何想法在这里出了什么问题?

谢谢!

解决方法

beforeSend钩子只能用于$.ajaxSetup(),所以如果你只想添加到$.ajax(),你可以使用headers属性.

如果要将自定义标头(或一组标头)添加到单个请求中,那么只需添加headers属性

// Request with custom header
$.ajax({
    url: 'foo/bar',headers: { 'x-my-custom-header': 'some value' }
});

如果要为每个请求添加认标头(或一组标题),请使用$.ajaxSetup():

$.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钩子:

$.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' } });

编辑(更多信息):有一点需要注意的是,使用ajaxSetup,您只能定义一组标题,您只能定义一个beforeSend.如果您多次调用ajaxSetup,则只会发送最后一组标头,并且只会执行最后一个发送回调.

资料来源:https://stackoverflow.com/a/14655768/1581725

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


jQuery表单验证提交:前台验证一(图文+视频)
jQuery表单验证提交:前台验证二(图文+视频)
jQuery如何实时监听获取input输入框的值
JQuery怎么判断元素是否存在
jQuery如何实现定时重定向
jquery如何获取复选框选中的值
jQuery如何清空form表单数据
jQuery怎么删除元素节点
JQuery怎么循环输出数组元素
jquery怎么实现点击刷新当前页面