从http标头响应中获取日期
好的,我可以使用访问HTTP ajax响应头
xhr.getAllResponseHeaders();
但它似乎没有得到它的日期,虽然它在那里:
[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
并且代码仅显示以下内容:
[output on alert xhr.getAllResponseHeaders();] Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1
这是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); } });
有没有办法在响应标题中获取日期?
可能是您正在发出CORS请求并且出于安全原因过滤掉标头。
另请参阅关于ajax请求中缺少响应头的类似问题。 解决方案可能是在服务器响应中设置此HTTP标头:
Access-Control-Expose-Headers: Date
这有助于:
var req = new XMLHttpRequest(); req.open('GET', document.location, false); req.send(null); var headers = req.getAllResponseHeaders().toLowerCase(); alert(headers);
在JavaScript中访问网页的HTTP标头
在你的成功方法中:
success: function (data,status, xhr) { console.log(xhr.getResponseHeader('Date')); },
如果回应成功
res=xhr.getResponseHeader('Date');
如果响应失败
res=data.getResponseHeader('Date');