問題描述
我有一個通過 ajax 提交的表單.我正在使用 jquery 表單插件.我想要做的是獲取從我的服務器返回的位置"標頭.我可以在螢火蟲中看到它.但是每當我在成功回調中調用 getResponseHeader() 函數時,它總是返回未定義"..
I have a a form that I am submitting via ajax. I am using the jquery form plugin. What I am trying to do is get the 'Location' header which is returned from my server. I can see it in firebug. But whenever I call the getResponseHeader() function in my success callback, it always returns 'undefined'..
代碼:
form.ajaxForm({
dataType: 'xml',
data: {format: 'xml'},
resetForm: true,
success: function(xml,status,xhr){
var location = xhr.getResponseHeader('Location');
alert(location);
});
位置未定義.但我可以在螢火蟲中看到位置"標題.我錯過了什么?即使我從 xhr 對象調用 getAllResponseHeaders(),它也會返回 'undefined'
location is undefined. But I can see the 'Location' header in firebug. What am I missing? Even if I call getAllResponseHeaders() from the xhr object, it returns 'undefined'
推薦答案
如果這是 CORS 請求,你可能會在調試工具(例如 Chrome->Inspect Element->Network)中看到所有標頭,但 xHR 對象只會檢索標頭(通過 xhr.getResponseHeader('Header')
) 如果這樣的標頭是簡單響應標頭:
If this is a CORS request, you may see all headers in debug tools (such as Chrome->Inspect Element->Network), but the xHR object will only retrieve the header (via xhr.getResponseHeader('Header')
) if such a header is a simple response header:
內容類型
最后修改
內容語言
緩存控制
過期
語用
如果不在此集合中,則它必須存在于 Access-Control-Expose-Headers 服務器返回的標頭.
If it is not in this set, it must be present in the Access-Control-Expose-Headers header returned by the server.
關于所討論的情況,如果它是一個 CORS 請求,則只有當且僅當,才能通過 XMLHttpRequest
對象檢索 Location
標頭下面的標題也存在:
About the case in question, if it is a CORS request, one will only be able to retrieve the Location
header throgh the XMLHttpRequest
object if, and only if, the header below is also present:
Access-Control-Expose-Headers: Location
如果它不是 CORS 請求,XMLHttpRequest
將沒有問題檢索它.
If its not a CORS request, XMLHttpRequest
will have no problem retrieving it.
這篇關于jquery getResponseHeader 總是返回“未定義"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!