gpt4 book ai didi

javascript - React native 能够使用 access_token 进行 GET 但无法 POST,它返回 "Unauthenticated."

转载 作者:行者123 更新时间:2023-11-30 19:40:54 26 4
gpt4 key购买 nike

我使用 Axios GET 并将 header access_token 传递给 Laravel api,它工作正常。在我创建了另一个使用 POST 的方法之后,我创建了 Axios POST,并在 header access_token 中传递,但它返回错误“未验证”。我可以知道如何解决这个问题吗?谢谢。

我试过用同样的方法把GET改成POST,GET成功了,POST还是报错“Unauthenticated.”。

 getEmployeeClockInPosition = async () => {
let GET_EMPLOYEE_COMPANY_POSITION = 'http://localhost/get-employee-company-location';
await axios.post(GET_EMPLOYEE_COMPANY_POSITION, {
headers: {
'Content-Type' : 'application/json',
'Authorization' : 'Bearer '+ this.props.authState.accessToken,
'Accept' :'application/json',
},
userID: this.props.authState.id,
})
.then(res => {
console.log(res.data);
})
.catch(function(error) {
console.log(error);
});
}
Route::group(['middleware' => 'auth:api'], function() {
Route::post('/get-employee-company-location','Clocking\DashboardController@getEmployeeCompanyLocation');
});

最佳答案

据官方documentation , axios.post 的参数应该按照这个顺序:

axios.post(url[, data[, config]])

在您的情况下,您在数据之前提供后期配置。

请试试这个方法:

let postData = {
userID: this.props.authState.id
}

let config = {
headers: {
'Content-Type' : 'application/json',
'Authorization' : 'Bearer '+ this.props.authState.accessToken,
'Accept' :'application/json'
}
}

axios.post(GET_EMPLOYEE_COMPANY_POSITION, postData, config)

关于javascript - React native 能够使用 access_token 进行 GET 但无法 POST,它返回 "Unauthenticated.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55415210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com