草庐IT

javascript - XmlHttpRequest getAllResponseHeaders() 不返回所有 header

coder 2024-05-10 原文

我正在尝试从 ajax 请求中获取响应 header ,但 jQuery 的 getAllResponseHeaders xhr 方法仅显示“Content-Type” header 。有人知道为什么吗?

这是响应头
访问控制允许凭据:true
Access-Control-Allow-Headers:If-Modified-Since, Cache-Control, Content-Type, Keep-Alive, X-Requested-With, Authorization
访问控制允许方法:GET、PUT、POST、DELETE、OPTIONS
访问控制允许来源:*
访问控制最大年龄:1728000
授权:apikey="apikey1"AuthenticationToken="62364GJHGJHG"
连接:保持事件状态
内容长度:240
内容类型:application/json;字符集=utf-8
X-Powered-By:Express

这是成功函数

params.success = function (response, textStatus, jqXHR) {
  console.log(jqXHR.getAllResponseHeaders())
}

这是它记录的内容......
内容类型:application/json;字符集=utf-8

最佳答案

刚遇到这个。这是因为您正在执行 CORS 请求并且没有公开 Location header 。

您需要在 Express 中将 Access-Control-Expose-Headers 添加到您的预检 CORS 响应中:

res.header('Access-Control-Expose-Headers', 'Content-Type, Location');
res.send(200);

这将解决问题。

关于javascript - XmlHttpRequest getAllResponseHeaders() 不返回所有 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8945587/

有关javascript - XmlHttpRequest getAllResponseHeaders() 不返回所有 header的更多相关文章

随机推荐