Get browser response headers for a request

Suppose index.html has a script that has a url for an external js file (example.js):

<html>
<head>
    <script src="/example.js"></script>
</head>
<body></body>
</html>

I tried creating XMLHttpRequest and manually executing the script with window.eval(request.responseText). Any other ways?

+4
source share
1 answer

To get response headers when you make a server request:

Vanilla JS :

var client = new XMLHttpRequest();
client.open("GET", "/some_url", true);
client.send();
client.onreadystatechange = function() {
    if (this.readyState == this.HEADERS_RECEIVED) {
        console.log(client.getResponseHeader("some_header"));
    }
}

JQuery

$.ajax({
    type: 'GET',
    url: '/some_url',
    success: function(data, textStatus, request) {
        console.log(request.getResponseHeader('some_header'));
    },
    error: function(request, textStatus, errorThrown) {
        console.log(request.getResponseHeader('some_header'));
    }
});
0
source

Source: https://habr.com/ru/post/1691203/


All Articles