Axios可以访问响应头字段 [英] Axios get access to response header fields

查看:1553
本文介绍了Axios可以访问响应头字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用React和Redux构建一个前端应用程序,我正在使用



Bests。

解决方案

如果是CORS请求,浏览器默认只能访问以下响应头:




  • Cache-Control

  • 内容 - 语言

  • 内容类型

  • 到期

  • 最后-Modified

  • Pragma



如果您希望您的客户端应用能够访问其他标题,你需要设置 Access-Control-Expose-Headers 标题:

  Access-Control-Expose-Headers:Access-Token,Uid 


I'm building a frontend app with React and Redux and I'm using axios to perform my requests. I would like to get access to all the fields in the header of the response. In my browser I can inspect the header and I can see that all the fields that I need are present(such as token, uid, etc...), but when I call

const request = axios.post(`${ROOT_URL}/auth/sign_in`, props);
request.then((response)=>{
  console.log(response.headers);
});

I get just

Object {content-type: "application/json; charset=utf-8", cache-control: "max-age=0, private, must-revalidate"}

Here my browser network tab,as you can see all the other fields are present.

Bests.

解决方案

In case of CORS requests, browsers can only access the following response headers by default:

  • Cache-Control
  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma

If you would like your client app to be able to access other headers, you need to set the Access-Control-Expose-Headers header on the server:

Access-Control-Expose-Headers: Access-Token, Uid

这篇关于Axios可以访问响应头字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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