jquery getResponseHeader 总是返回“未定义"?

我有一个通过 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.

相关文章