1

我尝试像这样获取标题值“日期”,但它给出了:

xhr.getResponseHeader is not a function 

我在萤火虫中看到响应标头并且它存在: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;
}

从萤火虫调试转储,网址: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  ....

控制台日志(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)

控制台日志(输出)

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

控制台日志(状态)

error
4

1 回答 1

2

always 的签名是

.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')
于 2013-08-09T00:42:16.407 回答