【发布时间】:2017-05-22 00:09:37
【问题描述】:
我正在尝试将 axios 用于带有 API 的 GET 请求,该 API 需要 Authorization 标头。
我当前的代码:
const AuthStr = 'Bearer ' + USER_TOKEN;
其中USER_TOKEN 是所需的访问令牌。这个字符串连接可能是问题,好像我将其发布为AuthStr = 'Bearer 41839y750138-391',以下 GET 请求有效并返回我所追求的数据。
axios.get(URL, { 'headers': { 'Authorization': AuthStr } })
.then((response => {
console.log(response.data);
})
.catch((error) => {
console.log(error);
});
我也尝试将其设置为全局标头,但没有成功。
【问题讨论】:
-
console.log('Bearer' + USER_TOKEN) 给出了什么?
-
它给 Bearer 472397403110(或任何令牌号)
-
console.log(typeof(USER_TOKEN)) 给出了什么?
-
尝试使用concat。这是字符串连接推荐的javascript方式
-
使用 concat 没有改变。并且 typeof 给出字符串返回
标签: react-native oauth-2.0 http-headers fetch axios