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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取浏览器创建的请求的响应标头相关的知识,希望对你有一定的参考价值。

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

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

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

答案

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

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

以上是关于获取浏览器创建的请求的响应标头的主要内容,如果未能解决你的问题,请参考以下文章