繁体   English   中英

使用Axios GET发送授权令牌不起作用

[英]Sending authorization token with Axios GET does not work

我在我的本机应用程序中发出GET请求。 我的代码是:

const config = {
  headers: {
    Authorization: `Bearer ${authToken}`,
  },
};
axios.get(url, config);

授权令牌未与请求一起发送。 我该如何发送?

您可以通过两种方式使用相同的GET方法。

方法1

axios.get(your_url, {
 headers: {
   Authorization: 'Bearer ' + your_token
 }
})

方法2

axios({
  method: 'get', 
  url: your_url,
  headers: {
    Authorization: 'Bearer ' + your_token
  }
})

对于POST方法

axios({
      method: 'post', 
      url: your_url,
      data: { "user_id": 1 }
      headers: {
        Authorization: 'Bearer ' + your_token
      }
    })

请尝试使用上述任何一种方法,并让我知道。

尝试这个:

var req = {
            url: the url ,
            method: "get",
            headers: {
                Authorization: "Bearer " + val,
                Accept: "application/json"
            }
        };

        axios(req)
            .then(response=>{console.log(response)})
            .catch(error => {});
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM