jQuery的getResponseHeader总是返回“未定义”? [英] jquery getResponseHeader always returns 'undefined'?
问题描述
我有一个,我通过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'..
code:
form.ajaxForm({
dataType: 'xml',
data: {format: 'xml'},
resetForm: true,
success: function(xml,status,xhr){
var location = xhr.getResponseHeader('Location');
alert(location);
});
的位置是不确定的。但我可以看到萤火虫的位置标头。我在想什么?即使我打电话从XHR对象getAllResponseHeaders(),它返回未定义
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申请,你可能会看到在调试工具的所有标题(如铬 - >检查元素 - >网络),但是XHR对象将只检索头(通过 xhr.getResponseHeader('头')
),如果这样的标题是简单的响应头:
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:
-
内容类型
-
上次修改
-
内容语言
-
的Cache-Control
-
过期
-
语用
Content-Type
Last-modified
Content-Language
Cache-Control
Expires
Pragma
如果不是在这一套,它必须是present的<一个href="http://www.w3.org/TR/cors/#access-control-expose-headers-response-header">Access-Control-Expose-Headers header服务器返回。
If it is not in this set, it must be present in the Access-Control-Expose-Headers header returned by the server.
有关问题的情况下,如果是CORS请求,一会只能检索位置
throgh的 XMLHtt $ P头$ pquest
对象,当且仅当,下面的标题也是present:
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请求, XMLHtt prequest
不会有任何问题,获取它。
If its not a CORS request, XMLHttpRequest
will have no problem retrieving it.
这篇关于jQuery的getResponseHeader总是返回“未定义”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!