XmlHttpRequest getAllResponseHeaders() 未返回所有标头

发布于 2024-12-28 00:32:53 字数 671 浏览 1 评论 0原文

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

这是响应标头
访问控制允许凭据:true
访问控制允许标头:If-Modified-Since、缓存控制、内容类型、保持活动、X-Requested-With、授权
访问控制允许方法: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

I'm trying to get the response headers from an ajax request but jQuery's getAllResponseHeaders xhr method only displays the "Content-Type" header. Anyone know why?

This is the response header
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:If-Modified-Since, Cache-Control, Content-Type, Keep-Alive, X-Requested-With, Authorization
Access-Control-Allow-Methods:GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1728000
Authorization:apikey="apikey1" AuthenticationToken="62364GJHGJHG"
Connection:keep-alive
Content-Length:240
Content-Type:application/json; charset=utf-8
X-Powered-By:Express

This is the success function

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

This is what it logs...
Content-Type: application/json; charset=utf-8

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金兰素衣 2025-01-04 00:32:53

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

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

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

这将解决问题。

Just ran into this. It's because you're doing a CORS request and you're not exposing the Location header.

You need to add a Access-Control-Expose-Headers to your preflight CORS response in Express:

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

That will solve the issue.

何以心动 2025-01-04 00:32:53

根据以下

https://developer .mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers

Access-Control-Expose-Headers 响应标头通过列出其名称来指示哪些标头可以作为响应的一部分公开。

默认情况下,仅公开 7 个 CORS 安全列表响应标头:

Cache-Control
Content-Language
Content-Length
Content-Type
Expires
Last-Modified
Pragma

因此,这对于可访问和公开所有标头来说非常有效

res.header('Access-Control-Expose-Headers', '*');

according to the following

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers

The Access-Control-Expose-Headers response header indicates which headers can be exposed as part of the response by listing their names.

By default, only the 7 CORS-safelisted response headers are exposed:

Cache-Control
Content-Language
Content-Length
Content-Type
Expires
Last-Modified
Pragma

So this will work perfectly for all headers to be accessible and exposed

res.header('Access-Control-Expose-Headers', '*');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文