获取头文件Ajax Jquery

时间:2013-08-09 00:35:27

标签: javascript jquery ajax

我尝试像这样得到标题值“Date”,但它给出了:

xhr.getResponseHeader is not a function 

我在firebug中看到响应头并且它存在:S

也许没有JQuery支持?可以用JavaScript完成吗?必须工作,我可以看到标题......

代码:

function ajaxDate(myUrl){   
var res;    
var ajaxCall=$.ajax({
        type: 'GET',
        url: myUrl,
    crossDomain: true,
        async: false,
        cache: false
    }).always(function(output, status, xhr) {
            //alert(xhr.getResponseHeader("MyCookie"));
        console.log(xhr);
        console.log(output);
        console.log(status);
    res=xhr.getResponseHeader('Date');
        });
    return res;
}

来自firebug的调试转储,网址:www.google.se:

200 OK 92ms jquery.min.js(第5行)

响应标头

Alternate-Protocol  80:quic
Cache-Control   private, max-age=0
Content-Encoding    gzip
Content-Type    text/html; charset=UTF-8
Date    Fri, 09 Aug 2013 00:57:43 GMT
Expires -1
P3P CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server  gws
Set-Cookie  PREF=ID=e6503cda76a:FF=0:TM=1376009863:LM=1376009863:S=pByclnZqvnZs2k5S; expires=Sun, 09-Aug-2015 00:57:43 GMT; path=/; domain=.google.se, expires=Sat, 08-Feb-2014 00:57:43 GMT; path=/; domain=.google.se; HttpOnly
Transfer-Encoding   chunked
x-frame-options SAMEORIGIN
x-xss-protection    1; mode=block

请求标题

Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Host    www.google.se
Origin  http://localhost/
Referer http://localhost/
User-Agent  ....

的console.log(XHR)

[Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js :: .send :: line 5" data: no] { name="NS_ERROR_FAILURE", message="Failure", result=2147500037, more...}
auto.js (line 52)

的console.log(输出)

Object { readyState=0, status=0, statusText="[Exception... "Failure"...d :: line 5" data: no]"}
auto.js (line 53)

的console.log(状态)

error

1 个答案:

答案 0 :(得分:2)

总是签名

.always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { });

如果映射到您的方法

.always(function(output, status, xhr) {

这一行

res=xhr.getResponseHeader('Date'); // If response is a success

应该是

res=output.getResponseHeader('Date'); // if response fails

您的方法看起来失败,将相同的方法映射到errorThrown

相反,您可以编写一个单独的fail方法来处理它或对语法进行小的更改。

res= output.getResponseHeader ? output.getResponseHeader.get('Date')
                              : xhr.getResponseHeader.get('Date')