jquery getResponseHeader 总是返回“未定义”?
我有一个通过 ajax 提交的表单。我正在使用 jquery 表单插件。我想做的是获取从我的服务器返回的“位置”标头。我可以在萤火虫中看到它。但是每当我在成功回调中调用 getResponseHeader() 函数时,它总是返回“未定义”。
代码:
form.ajaxForm({
dataType: 'xml',
data: {format: 'xml'},
resetForm: true,
success: function(xml,status,xhr){
var location = xhr.getResponseHeader('Location');
alert(location);
});
位置未定义。但我可以在萤火虫中看到“位置”标头。我缺少什么?即使我从 xhr 对象调用 getAllResponseHeaders() ,它也会返回“未定义”
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);
});
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这是 CORS 请求,您可能会在调试工具(例如 Chrome- >Inspect Element->Network),但如果此类标头是 简单响应标头:
Content-Type
Last-modified
Cache-Control
Expires
Pragma
如果不在该集合中,则必须存在在 Access-Control-Expose-Headers 服务器返回的标头。
对于上述情况,如果是 CORS 请求,则只能通过
XMLHttpRequest
Location 标头> object 当且仅当下面的标头也存在时:如果它不是 CORS 请求,
XMLHttpRequest
检索它不会有问题。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: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-Headers header returned by the server.
About the case in question, if it is a CORS request, one will only be able to retrieve the
Location
header throgh theXMLHttpRequest
object if, and only if, the header below is also present:If its not a CORS request,
XMLHttpRequest
will have no problem retrieving it.XMLHttpRequest 将透明地遵循重定向,因此最终请求不会有标头,它已经遵循了该重定向,并且您会看到来自该请求的响应标头(不是具有
的初始请求位置
标头)。An XMLHttpRequest will transparently follow a redirect, so the final request won't have the header, it's already followed that redirect and you're seeing the response headers from that request (not the initial request which had the
Location
header).我正在使用rails/rest 方法做类似的事情,将带有位置标头的 201“创建”返回到新对象和空主体。 jQuery 的 ajax 方法在遇到这种情况时会抛出一个“parseerror”,因为它期望 json 但没有返回任何结果。我只是在错误回调中捕获 201 重定向,如下所示:
希望这有帮助!
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:
hope this helps!