从http标头响应中获取日期

End*_*ode 5 javascript ajax jquery http-headers

好的,我可以使用访问HTTP ajax响应头

xhr.getAllResponseHeaders();
Run Code Online (Sandbox Code Playgroud)

但它似乎没有得到它的日期,虽然它在那里:

 [Chrome]
**Response Header**
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Length:8092
Content-Type:application/json; charset=utf-8
**Date:Thu, 15 Jan 2015 16:30:13 GMT**
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
TotalCount:116
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
Run Code Online (Sandbox Code Playgroud)

并且代码仅显示以下内容:

[output on alert xhr.getAllResponseHeaders();]

Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Run Code Online (Sandbox Code Playgroud)

这是ajax电话:

   $.ajax({
        url: url,
        type: "GET",
        contentType: "application/json;charset=utf-8",
        async: true,
        success: function (data,status, xhr) {

        displayNewData(data);
        alert(xhr.getAllResponseHeaders());

    },
    error: function () {
    alert(url);

    }
});
Run Code Online (Sandbox Code Playgroud)

有没有办法在响应标题中获取日期?

小智 6

可能是您正在发出CORS请求并且出于安全原因过滤掉标头.

另请参阅关于ajax请求中缺少响应头的类似问题.解决方案可能是在服务器响应中设置此HTTP标头:

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


End*_*ode 3

这有帮助:

var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
alert(headers);
Run Code Online (Sandbox Code Playgroud)

在 JavaScript 中访问网页的 HTTP 标头