Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Date from http header response

Okay so I can access the HTTP ajax response header using

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({
        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?

like image 998
EnderCode Avatar asked Jan 15 '15 16:01

EnderCode


2 Answers

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

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

Access-Control-Expose-Headers: Date
like image 142
Keywan Ghadami Avatar answered Oct 04 '22 00:10

Keywan Ghadami


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

like image 22
EnderCode Avatar answered Oct 04 '22 02:10

EnderCode