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

发布于 2024-10-06 23:17:17 字数 450 浏览 2 评论 0原文

我有一个通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

早乙女 2024-10-13 23:17:17

如果这是 CORS 请求,您可能会在调试工具(例如 Chrome- >Inspect Element->Network),但如果此类标头是 简单响应标头:

  • Content-Type
  • Last-modified
  • < code>Content-Language
  • Cache-Control
  • Expires
  • Pragma

如果不在该集合中,则必须存在在 Access-Control-Expose-Headers 服务器返回的标头。

对于上述情况,如果是 CORS 请求,则只能通过 XMLHttpRequestLocation 标头> object 当且仅当下面的标头也存在时:

Access-Control-Expose-Headers: Location

如果它不是 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 the XMLHttpRequest object if, and only if, the header below is also present:

Access-Control-Expose-Headers: Location

If its not a CORS request, XMLHttpRequest will have no problem retrieving it.

星星的軌跡 2024-10-13 23:17:17

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).

番薯 2024-10-13 23:17:17

我正在使用rails/rest 方法做类似的事情,将带有位置标头的 201“创建”返回到新对象和空主体。 jQuery 的 ajax 方法在遇到这种情况时会抛出一个“parseerror”,因为它期望 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
}

希望这有帮助!

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:

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文