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

最近用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');

猜你喜欢

转载自blog.csdn.net/adsadadaddadasda/article/details/79564683