XmlHttpRequest getAllResponseHeaders()不返回所有标头

编程入门 行业动态 更新时间:2024-10-27 10:22:21
本文介绍了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()不返回所有标头

本文发布于:2023-10-11 04:36:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1480703.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:XmlHttpRequest   getAllResponseHeaders

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!