【发布时间】:2019-12-18 14:36:08
【问题描述】:
我正在尝试在 React 中使用 Axios 发出发布请求,但我收到了未经授权的错误:
我有一个 API 叫做:
12.0.0.1:8000/api
我正在尝试使用 Axios 发出发布请求,因为我正在使用此代码:
authenticate(signInForm)
.then(res => {
console.log(res.data.token);
let token = res.data.token;
console.log(token);
// $.ajax()
axios.post(
'http://12.0.0.1:8000/api/',
{
headers: {
'Content-Type' : 'application/json',
'Accept' : 'application/json',
'Authorization' : `JWT ${token}`
},
},
{
question_name: 'I am the the first question',
user_id: '1',
session_id: '1',
data: {
hello: ['armani', 'gucci'],
},
created: '2019-12-17T10:56:29.332637Z',
},
);
})
.catch(err => {
alert('failed to login');
});
在这段代码中,当我像这样粘贴该令牌时,我可以获得确切的令牌:
'Authorization' : `JWT <token>`
我的代码正在运行我不知道我尝试将其保存到本地存储使用时间间隔的问题是什么。在邮递员中,相同的查询工作正常。但在这里我没有取得任何成功。
【问题讨论】: