使用jQuery.ajax和JSONP设置标题? [英] Set Headers with jQuery.ajax and JSONP?
问题描述
我想用jQuery访问google docs。这是我到目前为止:
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
(来自使用jQuery创建跨域Ajax请求)。如果我省略 jsonp
,我无法提出跨网域请求。如果我使用 jQuery.getJSON
,我不能传入任何标头...
在创建跨网域ajax请求时(在jQuery中)定义自定义标头?
这是不可能的。 >
JSONP请求的工作原理是创建一个< script>
元素及其 src
属性设置为请求URL。
不能向由< script>
元素发送的HTTP请求添加自定义标头。 p>
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) {
}
});
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...
Is there any way to define custom headers when making a cross-domain ajax request (in jQuery)?
This is not possible.
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屋!