7

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?

EnderCode
  • 235
  • 1
  • 3
  • 12

4 Answers4

7

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
Community
  • 1
  • 1
3

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

Community
  • 1
  • 1
EnderCode
  • 235
  • 1
  • 3
  • 12
  • It won't works as you can't access Date field using 'getAllResponseHeaders' or 'getResponseHeader()' Chrome throw an error 'Refused to get unsafe header "date"' – stefbach Sep 30 '15 at 05:33
1

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');
pearpages
  • 18,703
  • 1
  • 26
  • 27
  • I need to get the current time of the server which seems to be in the response Header – EnderCode Jan 15 '15 at 16:45
  • Check nothing, you didn't answer my question, and you deleted your comment telling me to give a parameter with type String for a javascript function.. I wish I can vote you down. – EnderCode Jan 15 '15 at 18:38
  • Yes, I've tried it before posting the question, it gives null, clearly because it wont get other details than the ones I mentioned in my question.. – EnderCode Jan 15 '15 at 18:45
  • If my question doesn't deserve the vote, you shouldn't have had voted. If you were trying to be nice thanks. – EnderCode Jan 15 '15 at 18:47
  • I clearly see a date field in the response. Which one are you talking about? Otherwise just get the time from the server as part of json data response – pearpages Jan 15 '15 at 18:47
  • Does it also give null in the console.log? It works for me. I know it sounds silly... But it's just about trying – pearpages Jan 15 '15 at 18:57
0

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

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".

enter image description here

Meirza
  • 1,318
  • 10
  • 15