Accessing HTTP Headers
How do you access the server responses from a web page using JavaScript? The native response headers are not directly accessible. However, you can use the technique below to obtain them through a different HTTP request.
JavaScript Code for Retrieving HTTP Headers
The following code snippet demonstrates how to use JavaScript to obtain all HTTP headers for the current page by simulating a GET request:
var req = new XMLHttpRequest(); req.open('GET', document.location, true); req.send(null); req.onload = function() { var headers = req.getAllResponseHeaders().toLowerCase(); console.log(headers); };
Note
It's important to remember that this method retrieves the headers from the server, which may not be identical to the original headers received by the browser.
The above is the detailed content of How Can I Access HTTP Headers from a Web Page Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!