Axios在React Native中出现网络错误

nhaq1z21  于 2022-11-05  发布在  iOS
关注(0)|答案(2)|浏览(330)

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

wgxvkvu9

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)

try{
 const {data} =  await axios.post(
        'https://backend.unknownchats.com/accounts/login/',
        {
            username,
            password,
        },
        {
            headers: {
            'Content-Type': "application/json",
            'Accept': "application/json",
            }  
        }   
     );
   console.log(data);      
 }catch(e){
   console.log(e.response.data);
 }

如果您能找到响应错误,请告诉我。
干杯!

ojsjcaue

ojsjcaue2#

const config = {
  method: 'post',
  url: `${BASE_URL}/login`,
  headers: { 
    'Content-Type': 'multipart/form-data'.  <----- Add this line in your axios header
  },
  data : formData
};

  axios(config).then((res)=> console.log(res))

相关问题