如何从xhr获取标题?

时间:2017-10-18 02:09:13

标签: javascript http xmlhttprequest

我正在尝试从标头响应中获取令牌,但我只获得“Content-Type”,我的JS代码非常简单

get(url){
    const xhr = new XMLHttpRequest();
    xhr.open("GET", url)
    xhr.addEventListener("readystatechange", function () {
        if (this.readyState == this.HEADERS_RECEIVED)
            console.log(this.getAllResponseHeaders())
    });
    xhr.send(null)
}

输出仅为“Content-Type:application / json”

如果我使用邮递员或浏览器我可以看到“MyHeader”,重要的是要强调我的请求是一个CORS请求

1 个答案:

答案 0 :(得分:1)

您需要设置相应的CORS标头以及从您请求的网址发送的响应。至少,你需要这样的东西:

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST, GET");   
header('Access-Control-Expose-Headers: Custom-Header-Name');