我试图用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,errorThrown) {
}
});
它不允许我设置标题,如果我将dataType设置为jsonp(从Make Cross Domain Ajax Requests with jQuery)。如果我离开jsonp,我不能做跨域请求。如果我使用jQuery.getJSON,我不能传入任何标头…
有什么方法来定义自定义标题,当做一个跨域的ajax请求(在jQuery)?
解决方法
这不可能。
JSONP请求的工作原理是创建一个< script>元素,其src属性设置为请求URL。您无法将自定义标头添加到由< script>发送的HTTP请求。元件。