javascript 从 http 标头响应中获取日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27968368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:13:44  来源:igfitidea点击:

Getting Date from http header response

javascriptjqueryajaxhttp-headers

提问by EnderCode

Okay so I can access the HTTP ajax response header using

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

xhr.getAllResponseHeaders();

but it doesn't seem to get the Date with it, though its there:

但它似乎没有得到日期,尽管它在那里:

 [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

and the code only shows this :

并且代码只显示了这一点:

[output on alert xhr.getAllResponseHeaders();]

Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1

here's the the ajax call:

这是 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);

    }
});

Is there a way where I can get the Date in the response header?

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

采纳答案by EnderCode

This Helped :

这有助于:

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

Accessing the web page's HTTP Headers in JavaScript

在 JavaScript 中访问网页的 HTTP 标头

回答by Keywan Ghadami

It might be the case you are making a CORSrequest and the headers are filtered out for security reasons.

可能是您发出CORS请求并且出于安全原因过滤掉标头的情况。

See also similar question about missing response headers in ajax request. The solution might be to set this HTTP header in the server response:

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

Access-Control-Expose-Headers: Date

回答by Pere Pages

in your success method:

在您的成功方法中:

 success: function (data,status, xhr) {

    console.log(xhr.getResponseHeader('Date'));


},

If response is a success

如果响应成功

res=xhr.getResponseHeader('Date');

if response fails

如果响应失败

res=data.getResponseHeader('Date');

回答by Klanjabrik

If you are using Nginx, you can put below code in Nginx config file:

如果您使用的是 Nginx,您可以将以下代码放在 Nginx 配置文件中:

add_header 'Access-Control-Expose-Headers' 'Date';

for real config example:

对于实际配置示例:

location / {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Expose-Headers' 'Date';
    root /usr/local/nginx/html;
    index  index.html index.htm;
}

After restarting your nginx service, you can call getAllResponseHeaders again and it will show you the "Date".

重新启动 nginx 服务后,您可以再次调用 getAllResponseHeaders,它会显示“日期”。

enter image description here

在此处输入图片说明