设置头与jQuery.ajax和JSONP? [英] Set Headers with jQuery.ajax and JSONP?
问题描述
我试图访问谷歌文档与jQuery。这是我到目前为止有:
I am trying to access google docs with jQuery. Here's what I have so far:
var token = "my-auth-token";
$.ajax({
url: "http://docs.google.com/feeds/documents/private/full?max-results=1&alt=json",
dataType: 'jsonp',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "GoogleLogin auth=" + token);
},
success: function(data, textStatus, XMLHttpRequest) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
这并不让我设置的头,如果我设置了的dataType
到 JSONP
(从<一个href="http://www.myviewstate.net/blog/post/2010/02/26/Make-cross-domain-Ajax-requests-with-jQuery.aspx">Make跨域Ajax请求使用jQuery )。如果我离开了 JSONP
,我不能让跨域请求。如果我使用 jQuery.getJSON
,我不能传递任何标题...
It doesn't allow me to set headers if I set the dataType
to jsonp
(from Make Cross Domain Ajax Requests with jQuery). If I leave out jsonp
, I can't make the cross-domain request. If I use jQuery.getJSON
, I can't pass in any headers...
有没有什么办法让一个跨域Ajax请求(jQuery的),当定义自定义页眉?
Is there any way to define custom headers when making a cross-domain ajax request (in jQuery)?
推荐答案
这是不可能的。
一个JSONP请求,通过创建一个&LT;脚本&GT;
与元素的的src
属性设置为请求的URL 。照片
您不能添加自定义页眉被发送的HTTP请求的&LT;脚本&GT;
元素
A JSONP request works by creating a <script>
element with its src
attribute set to the request URL.
You cannot add custom headers to the HTTP request sent by a <script>
element.
这篇关于设置头与jQuery.ajax和JSONP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!