您的位置:首页 > 移动开发 > IOS开发

CORS跨域时axios无法获取服务器自定义的header信息

2018-03-15 10:48 756 查看
最近用vue.js+axios开发单页面应用时,需要把自定义信息(token,uid)放到response  header中返回,如下
<?php
header("token:www.uxuew.cn");
header("uid:100");
?>
然后在客户端获取在服务器端自定义的header信息:
const request = axios.post('http://vue.uxuew.cn/login`,props);
request.then((response)=>{
console.log(response.headers);
});
但是我得到的结果里却没有我自定义的值
Object {
content-type: "application/json; charset=utf-8",
cache-control: "max-age=0, private, must-revalidate"
}
在浏览器网络菜单里,去可以看到所有的response  header信息,包含服务器端自定义的。

原因:

在使用CORS方式跨域时,浏览器只会返回以下默认头部header:response  header
Content-Language
Content-Type
Expires
Last-Modified
Pragma
如果你想在客户端app中获取自定义的header信息,需要在服务器端header中添加Access-Control-Expose-Headers:
header('Access-Control-Expose-Headers:token,uid');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐