如何使用 jQuery 的 getJSON() 方法传递请求标头?
我需要执行 getJSON()
请求,但如何传递授权和自定义标头?
我遇到的问题是请求标头采用的是名称,而不是值。 URL 通过 fiddler 中的手动请求显示,并作为选项而不是 GET/Url 插入。
这是我们正在尝试做的一个例子,它在 fiddler 中运行良好;我怎样才能用 AJAX 功能复制这个?
GET /Service.svc/logins/gdd53535342/houses/vxcbdfsdg/people/dsgsdggd?format=json HTTP/1.1
User-Agent: Fiddler
Authorization: Basic rgbg423535fa23y4436
X-PartnerKey: df3fgeg-g5g6-b55b-f3d2-dsgg353523
Host: 154.34.53.54:2757
JavaScript 代码:
xhr = new XMLHttpRequest();
$(document).ready(function() {
$.ajax({
url: 'http://localhost:437/service.svc/logins/jeffrey/house/fas6347/devices?format=json',
type: 'GET',
datatype: 'json',
success: function() { alert("Success"); },
error: function() { alert('Failed!'); },
beforeSend: setHeader
});
});
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', 'Basic faskd52352rwfsdfs');
xhr.setRequestHeader('X-PartnerKey', '3252352-sdgds-sdgd-dsgs-sgs332fs3f');
}
Fiddler 正常请求标头:
GET /service.svc/logins/jeffrey/house/fas6347/devices?format=json HTTP/1.1
User-Agent: Fiddler
Authorization: Basic faskd52352rwfsdfs
X-PartnerKey: 3252352-sdgds-sdgd-dsgs-sgs332fs3f
Host: localhost:437
Fiddler 通过 Ajax()
请求标头:
OPTIONS service.svc/logins/jeffrey/house/fas6347/devices?format=json HTTP/1.1
Host: localhost:437
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6
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
Proxy-Connection: keep-alive
Origin: http://ipv4.fiddler:61975
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization,x-partnerkey
I need to do a getJSON()
request, but how do I pass authorisation and custom headers?
I am getting issues that the request header is taking the name, but NOT the values. The URL is being shown through a manual request in fiddler to being inserted in as options instead of GET/Url.
Here is an example of what we are trying to do that works fine in fiddler; how can I replicate this with the AJAX function?
GET /Service.svc/logins/gdd53535342/houses/vxcbdfsdg/people/dsgsdggd?format=json HTTP/1.1
User-Agent: Fiddler
Authorization: Basic rgbg423535fa23y4436
X-PartnerKey: df3fgeg-g5g6-b55b-f3d2-dsgg353523
Host: 154.34.53.54:2757
JavaScript code:
xhr = new XMLHttpRequest();
$(document).ready(function() {
$.ajax({
url: 'http://localhost:437/service.svc/logins/jeffrey/house/fas6347/devices?format=json',
type: 'GET',
datatype: 'json',
success: function() { alert("Success"); },
error: function() { alert('Failed!'); },
beforeSend: setHeader
});
});
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', 'Basic faskd52352rwfsdfs');
xhr.setRequestHeader('X-PartnerKey', '3252352-sdgds-sdgd-dsgs-sgs332fs3f');
}
Fiddler Normal Request Headers:
GET /service.svc/logins/jeffrey/house/fas6347/devices?format=json HTTP/1.1
User-Agent: Fiddler
Authorization: Basic faskd52352rwfsdfs
X-PartnerKey: 3252352-sdgds-sdgd-dsgs-sgs332fs3f
Host: localhost:437
Fiddler Through Ajax()
Request Headers:
OPTIONS service.svc/logins/jeffrey/house/fas6347/devices?format=json HTTP/1.1
Host: localhost:437
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6
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
Proxy-Connection: keep-alive
Origin: http://ipv4.fiddler:61975
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization,x-partnerkey
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我同意 sunetos 的观点,您必须使用 $.ajax 函数才能传递请求标头。为此,您必须为 beforeSend 事件处理程序编写一个函数,它是 $.ajax() 选项之一。以下是有关如何执行此操作的快速示例:
如果运行上面的代码并在 Fiddler 等工具中观察流量,您将看到传入的两个请求标头:
setHeader函数也可以内联在 $.ajax 选项中,但我想将其调用出来。
希望这有帮助!
I agree with sunetos that you'll have to use the $.ajax function in order to pass request headers. In order to do that, you'll have to write a function for the beforeSend event handler, which is one of the $.ajax() options. Here's a quick sample on how to do that:
If you run the code above and watch the traffic in a tool like Fiddler, you'll see two requests headers passed in:
The setHeader function could also be inline in the $.ajax options, but I wanted to call it out.
Hope this helps!
我认为您可以设置标头并仍然使用 getJSON() ,如下所示:
I think you could set the headers and still use getJSON() like this:
$.getJSON()
方法是简写方法,不允许您指定此类高级选项。为此,您需要使用完整的$.ajax()
方法。请注意文档 http://api.jquery.com/jQuery.getJSON/ :
因此只需使用
$.ajax()
并提供您需要的所有额外参数。The
$.getJSON()
method is shorthand that does not let you specify advanced options like that. To do that, you need to use the full$.ajax()
method.Notice in the documentation at http://api.jquery.com/jQuery.getJSON/:
So just use
$.ajax()
and provide all the extra parameters you need.