jquery getResponseHeader 总是返回“未定义”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4369987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery getResponseHeader always returns 'undefined'?
提问by demersus
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'..
我有一个通过ajax提交的表单。我正在使用 jquery 表单插件。我想要做的是获取从我的服务器返回的“位置”标头。我可以在萤火虫中看到它。但是每当我在成功回调中调用 getResponseHeader() 函数时,它总是返回“未定义”..
Code:
代码:
form.ajaxForm({
dataType: 'xml',
data: {format: 'xml'},
resetForm: true,
success: function(xml,status,xhr){
var location = xhr.getResponseHeader('Location');
alert(location);
});
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'
位置未定义。但是我可以在萤火虫中看到“位置”标题。我错过了什么?即使我从 xhr 对象调用 getAllResponseHeaders(),它也会返回“未定义”
回答by acdcjunior
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:
如果这是CORS 请求,您可能会在调试工具(例如 Chrome->Inspect Element->Network)中看到所有标头,但xhr.getResponseHeader('Header')
如果此类标头是简单的响应标头,则 xHR 对象将仅检索标头(via ):
Content-Type
Last-modified
Content-Language
Cache-Control
Expires
Pragma
Content-Type
Last-modified
Content-Language
Cache-Control
Expires
Pragma
If it is not in this set, it must be present in the Access-Control-Expose-Headersheader returned by the server.
如果它不在此集中,则它必须存在于服务器返回的Access-Control-Expose-Headers标头中。
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:
关于所讨论的案例,如果它是 CORS 请求,则只有当且仅当以下标头也存在时,才能Location
通过XMLHttpRequest
对象检索标头:
Access-Control-Expose-Headers: Location
If its not a CORS request, XMLHttpRequest
will have no problem retrieving it.
如果它不是 CORS 请求,XMLHttpRequest
则检索它没有问题。
回答by Nick Craver
An XMLHttpRequest will transparently follow a redirect, so the finalrequest won't have the header, it's already followed that redirect and you're seeing the response headers from thatrequest (not the initial request which had the Location
header).
XMLHttpRequest 将透明地跟随重定向,因此最终请求将没有标头,它已经跟随该重定向并且您看到的是来自该请求的响应标头(而不是具有Location
标头的初始请求)。
回答by esfourteen
I'm doing something similar using the rails/rest way of returning a 201 "created" with a Location header to the new object and an empty body. jQuery's ajax method will throw a "parseerror" when encountering this since its expecting json but getting nothing back. I simply catch the 201 redirect in my error callback like so:
我正在使用 rails/rest 方法将带有 Location 标头的 201 "created" 返回到新对象和一个空主体。jQuery 的 ajax 方法在遇到这个时会抛出一个“解析错误”,因为它期待 json 但什么也得不到。我只是在错误回调中捕获 201 重定向,如下所示:
function request_error(req, textStatus, errorThrown)
{
if (req.status == 201 ) {
var created_loc = req.getResponseHeader('Location');
console.log('(201) created: ' + created_loc);
// ... manual redirect here i.e. issue another ajax request to created_loc
return;
}
// ... handle an actual error here
}
hope this helps!
希望这可以帮助!