Getting Date from http header response

前端 未结 4 590
既然无缘
既然无缘 2021-01-12 09:17

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

相关标签:
4条回答
  • 2021-01-12 09:36

    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
    
    0 讨论(0)
  • 2021-01-12 09:37

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

    0 讨论(0)
  • 2021-01-12 09:49

    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');
    
    0 讨论(0)
  • 2021-01-12 09:51

    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

    0 讨论(0)
提交回复
热议问题