Accessing the web page's HTTP Headers in JavaScript

前端 未结 17 2529
日久生厌
日久生厌 2020-11-21 04:53

How do I access a page\'s HTTP response headers via JavaScript?

Related to this question, which was modified to ask about accessing two specific HTTP headers.

<
相关标签:
17条回答
  • 2020-11-21 05:37

    This is an old question. Not sure when support became more broad, but getAllResponseHeaders() and getResponseHeader() appear to now be fairly standard: http://www.w3schools.com/xml/dom_http.asp

    0 讨论(0)
  • 2020-11-21 05:38

    A solution with Service Workers

    Service workers are able to access network information, which includes headers. The good part is that it works on any kind of request, not just XMLHttpRequest.

    How it works:

    1. Add a service worker on your website.
    2. Watch every request that's being sent.
    3. Make the service worker fetch the request with the respondWith function.
    4. When the response arrives, read the headers.
    5. Send the headers from the service worker to the page with the postMessage function.

    Working example:

    Service workers are a bit complicated to understand, so I've built a small library that does all this. It is available on github: https://github.com/gmetais/sw-get-headers.

    Limitations:

    • the website needs to be on HTTPS
    • the browser needs to support the Service Workers API
    • the same-domain/cross-domain policies are in action, just like on XMLHttpRequest
    0 讨论(0)
  • 2020-11-21 05:38

    This is my script to get all the response headers:

    var url = "< URL >";
    
    var req = new XMLHttpRequest();
    req.open('HEAD', url, false);
    req.send(null);
    var headers = req.getAllResponseHeaders();
    
    //Show alert with response headers.
    alert(headers);
    

    Having as a result the response headers.

    This is a comparison test using Hurl.it:

    0 讨论(0)
  • 2020-11-21 05:39

    I think the question went in the wrong way, If you want to take the Request header from JQuery/JavaScript the answer is simply No. The other solutions is create a aspx page or jsp page then we can easily access the request header. Take all the request in aspx page and put into a session/cookies then you can access the cookies in JavaScript page..

    0 讨论(0)
  • 2020-11-21 05:42

    Like many people I've been digging the net with no real answer :(

    I've nevertheless find out a bypass that could help others. In my case I fully control my web server. In fact it is part of my application (see end reference). It is easy for me to add a script to my http response. I modified my httpd server to inject a small script within every html pages. I only push a extra 'js script' line right after my header construction, that set an existing variable from my document within my browser [I choose location], but any other option is possible. While my server is written in nodejs, I've no doubt that the same technique can be use from PHP or others.

      case ".html":
        response.setHeader("Content-Type", "text/html");
        response.write ("<script>location['GPSD_HTTP_AJAX']=true</script>")
        // process the real contend of my page
    

    Now every html pages loaded from my server, have this script executed by the browser at reception. I can then easily check from JavaScript if the variable exist or not. In my usecase I need to know if I should use JSON or JSON-P profile to avoid CORS issue, but the same technique can be used for other purposes [ie: choose in between development/production server, get from server a REST/API key, etc ....]

    On the browser you just need to check variable directly from JavaScript as in my example, where I use it to select my Json/JQuery profile

     // Select direct Ajax/Json profile if using GpsdTracking/HttpAjax server otherwise use JsonP
      var corsbypass = true;  
      if (location['GPSD_HTTP_AJAX']) corsbypass = false;
    
      if (corsbypass) { // Json & html served from two different web servers
        var gpsdApi = "http://localhost:4080/geojson.rest?jsoncallback=?";
      } else { // Json & html served from same web server [no ?jsoncallback=]
        var gpsdApi = "geojson.rest?";
      }
      var gpsdRqt = 
          {key   :123456789 // user authentication key
          ,cmd   :'list'    // rest command
          ,group :'all'     // group to retreive
          ,round : true     // ask server to round numbers
       };
       $.getJSON(gpsdApi,gpsdRqt, DevListCB);
    

    For who ever would like to check my code: https://www.npmjs.org/package/gpsdtracking

    0 讨论(0)
  • 2020-11-21 05:45

    Another way to send header information to JavaScript would be through cookies. The server can extract whatever data it needs from the request headers and send them back inside a Set-Cookie response header — and cookies can be read in JavaScript. As keparo says, though, it's best to do this for just one or two headers, rather than for all of them.

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