我正在尝试从ajax请求中获取响应标头,但jQuery的getAllResponseHeaders xhr方法仅显示 Content-Type标头。有人知道为什么吗?
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?
这是响应标头 Access-Control-Allow-Credentials:true Access-Control-Allow-标头:如果自修改,缓存控制,内容类型,保持活动,X请求使用,授权 Access-Control-Allow-方法:GET,PUT,POST,DELETE,OPTIONS Access-Control-Allow-Origin:* Access-Control-Max-Age:1728000 授权:apikey = apikey1 AuthenticationToken = 62364GJHGJHG 连接:keep-alive 内容长度:240 内容类型:application / json ; charset = utf-8 X-Powered-By:Express
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
这是成功函数
params.success = function (response, textStatus, jqXHR) { console.log(jqXHR.getAllResponseHeaders()) }这就是它的记录... 内容类型:application / json; charset = utf-8
This is what it logs... Content-Type: application/json; charset=utf-8
推荐答案刚刚遇到了这个问题。这是因为您正在执行CORS请求,并且没有公开Location标头。
Just ran into this. It's because you're doing a CORS request and you're not exposing the Location header.
您需要添加 Access-Control-Expose -Headers 到Express中您的飞行前CORS响应:
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);这将解决问题。
更多推荐
XmlHttpRequest getAllResponseHeaders()不返回所有标头
发布评论