简体   繁体   中英

Get response headers for request created by browser

Suppose index.html has script which has url to external js file ( example.js ):

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

What I have tried it's create XMLHttpRequest and than manually execute script with window.eval(request.responseText) . Any other ways?

To get the response headers when you make a request to server:

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'));
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM