在React Native中使用Axios发布请求的错误405

时间:2019-11-19 07:11:56

标签: react-native axios react-native-android expo

我正在EXPO React Native应用程序中使用axios发出发布请求,但我不知道为什么我的发布请求返回错误

  

请求失败,状态码为405

当我检查邮递员的API时,它向我显示了预期的结果。我附加了按下按钮上的 <LocationContext.Consumer> {({latitude, longitude}) => ( <View> ... </View> )} </LocationContext.Consumer> 的方法。请检查并提供解决方案。

onPress

2 个答案:

答案 0 :(得分:1)

好的,所以代码中有很多错误。

要在开始时回答您的问题,建议您添加withCredentials: true}

尝试在您的axios请求中添加{withCredentials: true}

第二,通过 async and await ,您无需使用.then和catch(而是使用try..catch

async onSubmit(ref) {
    if (ref.state.emailID && ref.state.password) {
    try {
       this.showLoader();
         const networkReq = await axios.post('http://apiurl.com/api/user/Userlogin?emailID=' + ref.state.emailID + '&password=' + ref.state.password,
          {withCredentials: true},
         { emailID: ref.state.emailID, password: ref.state.password },
         { headers: { 'Content-Type': 'application/json' } })
       this.hideLoader();
       console.log(networkReq.data) 
    } catch (error) {
      this.hideLoader();
      console.log("error: ", err.message);
    }
 }
}

答案 1 :(得分:0)

我犯了一个非常愚蠢的错误,请求已从post更改为get

相关问题