XmlHttpRequest getAllResponseHeaders()不返回所有标头 [英] XmlHttpRequest getAllResponseHeaders() not returning all the headers

查看:570
本文介绍了XmlHttpRequest getAllResponseHeaders()不返回所有标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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()不返回所有标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆