jQuery 和 AJAX 响应标头 [英] jQuery and AJAX response header

查看:46
本文介绍了jQuery 和 AJAX 响应标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I've got this jQuery AJAX call, and the response comes from the server in the form of a 302 redirect. I'd like to take this redirect and load it in an iframe, but when I try to view the header info with a javascript alert, it comes up null, even though firebug sees it correctly.

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

Here's the code, if it'll help:

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

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

I don't really have access to the server-side stuff in order to move the URL to the response body, which I know would be the easiest solution, so any help with the parsing of the header would be fantastic.

推荐答案

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

cballou's solution will work if you are using an old version of jquery. In newer versions you can also try:

  $.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'));
   }
  });

根据文档 XMLHttpRequest 对象从 jQuery 1.4 开始可用.

According to docs the XMLHttpRequest object is available as of jQuery 1.4.

这篇关于jQuery 和 AJAX 响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆