問題描述
我正在嘗試使用 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
(來自 使用 jQuery 進行跨域 Ajax 請求).如果我遺漏了 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 請求通過創建 <script>
元素并將其 src
屬性設置為請求 URL 來工作.
您不能向 <script>
元素發送的 HTTP 請求添加自定義標頭.
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 設置標題?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!