首页 > 解决方案 > 具有给定令牌的 Axios 拦截器,React

问题描述

在此请求之后:

   axios
  .post(url_auth, obj)
  .then(response => {

    const info = response.data
    this.setState({info})

  })
  .catch(error => {
    console.log(error);
  });

我在信息中存储了一个带有 id 和令牌的 JSON。(后台发给我)

我现在想将令牌存储在我提出的每个请求的标头中。

我该怎么做?我猜用拦截器?一旦我在拦截器中配置了标头(我试图了解如何),如何在我的请求中调用它?

非常感谢!

标签: reactjsxmlhttprequestaxiostokeninterceptor

解决方案


是的,这可以用拦截器来完成。参考这篇文章
你也可以参考我的一个仓库,该项目在 Angular 和 Node.Used Token 用于验证请求


推荐阅读