jQuery和AJAX响应头

Sha*_*ane 153 ajax jquery redirect header http-status-code-302

所以我有这个jQuery AJAX调用,响应来自服务器的302重定向形式.我想采用这种重定向并将其加载到iframe中,但是当我尝试使用javascript警报查看标题信息时,即使firebug正确地看到它,它也会出现空值.

这是代码,如果它有帮助:

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

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

rov*_*sen 171

如果您使用旧版本的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'));
   }
  });
Run Code Online (Sandbox Code Playgroud)

根据文档,XMLHttpRequest对象从jQuery 1.4开始提供.

  • 从jQuery> = 1.5开始,它应该被称为[jqXHR](http://api.jquery.com/Types/#jqXHR),它是XHR对象的超集. (5认同)

acd*_*ior 131

如果这是一个CORS请求,您可能会看到调试工具中的所有标头(例如Chrome-> Inspect Element-> Network),但是xhr.getResponseHeader('Header')如果这样的标头是一个简单的响应标头,xHR对象将只检索标头(via ):

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

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

关于这个案例,如果它是一个CORS请求,只有当下面的标题也存在时,才能Location通过该XMLHttpRequest对象检索标题:

Access-Control-Expose-Headers: Location
Run Code Online (Sandbox Code Playgroud)

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

  • @acdcjunior感谢你帮助我,在我花了一些时间找出为什么标题响应中没有输出 (3认同)

kei*_*ics 32

 var geturl;
  geturl = $.ajax({
    type: "GET",
    url: 'http://....',
    success: function () {
      alert("done!"+ geturl.getAllResponseHeaders());
    }
  });
Run Code Online (Sandbox Code Playgroud)

  • 对于那些不能像我一样工作的人.这可能是因为您正在进行跨域访问,而jquery不使用XHR.http://api.jquery.com/jQuery.get/ (9认同)

DRa*_*hal 18

关于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
Run Code Online (Sandbox Code Playgroud)

以下是该主题的一些stackoverflow帖子.一些帖子描述了解决这个问题的黑客行为.

如何在jQuery Ajax调用之后管理重定向请求

在JavaScript中捕获302 FOUND

HTTP重定向:301(永久)与302(临时)


Ple*_*and 10

jQuery使用的基础XMLHttpRequest对象将始终默默地遵循重定向,而不是返回302状态代码.因此,您无法使用jQuery的AJAX请求功能来获取返回的URL.相反,您需要将所有数据放入表单并提交表单,并将target属性设置为nameiframe 的属性值:

$('#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();
Run Code Online (Sandbox Code Playgroud)

服务器的url.do页面将加载到iframe中,但当其302状态到达时,iframe将被重定向到最终目标.


Cor*_*lou 9

试试这个:

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


sti*_*ows 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'));
        });
    }
Run Code Online (Sandbox Code Playgroud)