在 React Native 中使用 axios post 请求报错 405

Error 405 using axios post request in react native

我正在我的 expo react 本机应用程序中使用 axios 发出 post 请求,但我不知道为什么我的 post 请求返回错误

Request failed with status code 405

当我在 postman 上检查我的 API 时,它显示了预期的结果。我附加了在按钮上点击 onPress 的方法。请检查并提供解决方案。

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

好的,所以你的代码中有很多地方是错误的。

为了一开始就回答你的问题,我建议你加上withCredentials: true}

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

其次,使用 async 和 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);
    }
 }
}

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