I am using Expo Client for development and I am not able to send a login API call.
onSubmit fuction
const onSubmit = async (username, password)=>{
console.log(username);
console.log(password);
await axios.post(
'https://backend.unknownchats.com/accounts/login/',
{
username: username,
password: password,
},
{
headers: {
'Content-Type': "application/json",
'Accept': "application/json",
}
}
).then(res=>{
console.log(res.data);
if (res.data.status==="success"){
localStorage.setItem('username',res.data.username);
localStorage.setItem('token',res.data.token);
navigation.navigate('Settings');
}else{
setError(res.data.error);
}
}).catch(err=>console.log(err));
}
Error that I was getting
sumit
97127516
Network Error at node_modules\axios\lib\core\createError.js:17:22 in createError at node_modules\axios\lib\adapters\xhr.js:120:6 in handleError at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:609:10 in setReadyState at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:396:6 in __didCompleteResponse at node_modules\react-native\Libraries\vendor\emitter_EventEmitter.js:135:10 in EventEmitter#emit at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:414:4 in __callFunction at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:113:6 in __guard$argument_0 at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:365:10 in __guard at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:4 in callFunctionReturnFlushedQueue
2条答案
按热度按时间wgxvkvu91#
我测试过你的代码,没有语法错误。但是我注意到服务器没有响应请求。当用另一个baseUrl测试时,它工作得很好。但是用
https://backend.unknownchats.com/accounts/login/
我得到了和你一样的错误,网络错误,在失眠症和 Postman ,我得到了一个SSL错误。所以问题不在你的代码,而是服务器。我的建议是,在使用之前,先在Insomnia或Postman上测试一下你的API请求。
所以...首先,你要在同一个异步函数中使用“await”和“then”异步方法。
最好的方法是使用“await”..由try catch包围。
另一个提示是,当object key与prop传入的值相同时,可以只使用prop name,我会在上面设置。
第二点是,axios有自己的错误数据处理程序,你发送的错误是默认的。
所以,要查看错误数据,可以在catch中
console.log(error.response.data)
。如果您能找到响应错误,请告诉我。
干杯!
ojsjcaue2#