获取浏览器创建的请求的响应标头

问题描述 投票:4回答:1

假设index.html的脚本具有外部js文件的url(example.js):

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

我尝试过它创建XMLHttpRequest,而不是用window.eval(request.responseText)手动执行脚本。还有其他方法吗?

javascript xmlhttprequest eval
1个回答
0
投票

要在向服务器发出请求时获取响应标头:

香草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'));
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.