【问题标题】:jQuery and AJAX response headerjQuery 和 AJAX 响应标头
【发布时间】:2010-12-06 04:02:58
【问题描述】:

所以我得到了这个 jQuery AJAX 调用,并且响应以 302 重定向的形式来自服务器。我想采用此重定向并将其加载到 iframe 中,但是当我尝试使用 javascript 警报查看标头信息时,它显示为 null,即使 firebug 正确地看到它。

这是代码,如果有帮助的话:

$j.ajax({
    type: 'POST',
    url:'url.do',
    data: formData,
    complete: function(resp){
        alert(resp.getAllResponseHeaders());
    }
});

我真的无法访问服务器端的东西以将 URL 移动到响应正文,我知道这将是最简单的解决方案,因此任何有关解析标头的帮助都会很棒。

【问题讨论】:

  • 如果您在 2017 年或以后访问此问题,请不要在大多数现有答案上浪费时间。如果您的问题与 OP 相同,您有两个选择:1)设置代理服务器,它将 post 原始服务器并提取目标数据,前端 JS 将请求此代理服务器获取目标数据.或者,2) 更改服务器的代码以允许 CORS。

标签: jquery ajax redirect header http-status-code-302


【解决方案1】:

试试这个:

type: "GET",
async: false,
complete: function (XMLHttpRequest, textStatus) {
    var headers = XMLHttpRequest.getAllResponseHeaders();
}

【讨论】:

  • 嗯。非常感谢您的回复,但这仍然返回 null。还有其他想法吗?
  • 可能你使用的是旧版本的 jquery。
【解决方案2】:
 var geturl;
  geturl = $.ajax({
    type: "GET",
    url: 'http://....',
    success: function () {
      alert("done!"+ geturl.getAllResponseHeaders());
    }
  });

【讨论】:

  • 对我不起作用。也许我正在向另一个站点发出请求? (使用ajax的跨站请求)
  • 对于那些无法像我一样工作的人。这可能是因为您正在进行跨域访问,而 jquery 不使用 XHR。 api.jquery.com/jQuery.get
  • 像魅力一样醒来.. +1
【解决方案3】:

如果您使用的是旧版本的 jquery,cballou 的解决方案将有效。在较新的版本中,您也可以尝试:

  $.ajax({
   type: 'POST',
   url:'url.do',
   data: formData,
   success: function(data, textStatus, request){
        alert(request.getResponseHeader('some_header'));
   },
   error: function (request, textStatus, errorThrown) {
        alert(request.getResponseHeader('some_header'));
   }
  });

According to docs XMLHttpRequest 对象从 jQuery 1.4 开始可用。

【讨论】:

  • 从 jQuery >= 1.5 开始,它应该被称为 jqXHR,它是 XHR 对象的超集。
【解决方案4】:

jQuery will always silently follow redirects 使用的底层 XMLHttpRequest 对象,而不是返回 302 状态码。因此,您不能使用 jQuery 的 AJAX 请求功能来获取返回的 URL。相反,您需要将所有数据放入一个表单并提交表单,并将 target attribute 设置为 iframe 的 name 属性的值:

$('#myIframe').attr('name', 'myIframe');

var form = $('<form method="POST" action="url.do"></form>').attr('target', 'myIframe');
$('<input type="hidden" />').attr({name: 'search', value: 'test'}).appendTo(form);

form.appendTo(document.body);
form.submit();

服务器的url.do页面会被加载到iframe中,但是当它的302状态到达时,iframe会被重定向到最终目的地。

【讨论】:

    【解决方案5】:

    +1 到 PleaseStand 这是我的另一个技巧:

    搜索后发现“cross ajax request”无法从XHR对象获取响应头,我放弃了。并改用 iframe。

    1. <iframe style="display:none"></iframe>
    2. $("iframe").attr("src", "http://the_url_you_want_to_access")
    //this is my aim!!!
    3. $("iframe").contents().find('#someID').html()  
    

    【讨论】:

      【解决方案6】:

      关于 AJAX 和 302 重定向的不幸事实是,您无法从返回中获取标头,因为浏览器从不将它们提供给 XHR。当浏览器看到 302 时,它会自动应用重定向。在这种情况下,您会在 firebug 中看到标头,因为浏览器获取了它,但您不会在 ajax 中看到它,因为浏览器没有传递它。这就是为什么成功和错误处理程序永远不会被调用的原因。仅调用完整的处理程序。

      http://www.checkupdown.com/status/E302.html

      The 302 response from the Web server should always include an alternative URL to which redirection should occur. If it does, a Web browser will immediately retry the alternative URL. So you never actually see a 302 error in a Web browser
      

      这里有一些关于这个主题的 stackoverflow 帖子。一些帖子描述了解决此问题的技巧。

      How to manage a redirect request after a jQuery Ajax call

      Catching 302 FOUND in JavaScript

      HTTP redirect: 301 (permanent) vs. 302 (temporary)

      【讨论】:

        【解决方案7】:

        如果这是 CORS request,您可能会在调试工具中看到所有标头(例如 Chrome->Inspect Element->Network),但 xHR 对象将仅检索标头(通过 xhr.getResponseHeader('Header')),如果这样标头是simple response header:

        • Content-Type
        • Last-modified
        • Content-Language
        • Cache-Control
        • Expires
        • Pragma

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

        关于所讨论的情况,如果是 CORS 请求,只有当且仅当以下标头也存在时,才能通过 XMLHttpRequest 对象检索 Location 标头:

        Access-Control-Expose-Headers: Location
        

        如果它不是 CORS 请求,XMLHttpRequest 将没有问题检索它。

        【讨论】:

        • @acdcjunior 谢谢它也帮助了我,在我花了一些时间找出为什么标题响应中没有输出之后
        【解决方案8】:

        JQUERY 3 及更高版本的 2018 年更新

        我知道这是一个老问题,但上述解决方案都不适合我。这是有效的解决方案:

        //I only created this function as I am making many ajax calls with different urls and appending the result to different divs
        function makeAjaxCall(requestType, urlTo, resultAreaId){
                var jqxhr = $.ajax({
                    type: requestType,
                    url: urlTo
                });
                //this section is executed when the server responds with no error 
                jqxhr.done(function(){
        
                });
                //this section is executed when the server responds with error
                jqxhr.fail(function(){
        
                })
                //this section is always executed
                jqxhr.always(function(){
                    console.log("getting header " + jqxhr.getResponseHeader('testHeader'));
                });
            }
        

        【讨论】:

          猜你喜欢
          • 2012-10-09
          • 1970-01-01
          • 2014-03-30
          • 1970-01-01
          • 1970-01-01
          • 2018-02-17
          • 1970-01-01
          相关资源
          最近更新 更多