Axios在POST请求中返回401 [关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Axios在POST请求中返回401 [关闭]相关的知识,希望对你有一定的参考价值。

我不确定我的语法是否正确,或者值是否是导致服务器出现问题的值。

postDate = async () => { const token = await AsyncStorage.getItem('access'); const access = 'Bearer ' + token; axios.get(`http://laundry.test/api/auth/user`, { headers: { 'Authorization': access, } }).then(res => { const id = res.data.id; setID(id); }), axios.post('http://laundry.test/api/auth/saveDate', { headers: { 'Authorization': access, }, user_id: { id }, // trying to post this date: await AsyncStorage.getItem('Date'), // trying to post this }).then(res => { console.log(res); }) }

答案
您使用错误的发件人请求。这是正确的代码。

postDate = async () => { const token = await AsyncStorage.getItem('access'); const access = 'Bearer ' + token; axios.get(`http://laundry.test/api/auth/user`, { headers: { 'Authorization': access, } }).then(res => { const id = res.data.id; setID(id); }), axios.post('http://laundry.test/api/auth/saveDate', { user_id: { id }, date: await AsyncStorage.getItem('Date'), }, { headers: { 'Authorization': access, } }).then(res => { console.log(res); }) }

以上是关于Axios在POST请求中返回401 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章