我正在制作一个聊天应用程序。我有以下问题:当我向服务器发送请求时,响应不包含任何 header 。我想检查一些 header 的 token ,但该 header 是空的。
当我使用 postman 时,我在标题中看到了 token 。但是,当我在 javascript 中使用 fetch api 时,我看到了 header 但里面的 header 是空的。我还在谷歌开发者工具中看到了标题。
我认为,这与 CORS 无关。
static async userLogin(username,password){
const url = `http://localhost:9090/api/authenticate?username=${username}&password=${password}`;
await fetch(url,{
method: "GET",
}).then(response => console.log(response));
}
最佳答案
对于遇到相同问题的其他人,请使用 Access-Control-Expose-Headers它允许服务器将允许浏览器中的 Javascript(例如 getResponseHeader())访问的 header 列入白名单。
即使使用了 response.headers
使用 fetch
时为空当我切换到 XMLHttpRequest
并使用了getResponseHeaders() , 有效。
关于javascript - 响应 header 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144293/