XmlHttpRequest getAllResponseHeaders() 未返回所有标头
我试图从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
刚刚遇到这个。这是因为您正在执行 CORS 请求,并且没有公开 Location 标头。
您需要将
Access-Control-Expose-Headers
添加到 Express 中的预检 CORS 响应中:这将解决问题。
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:That will solve the issue.
根据以下
https://developer .mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
因此,这对于可访问和公开所有标头来说非常有效
according to the following
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
So this will work perfectly for all headers to be accessible and exposed