简体   繁体   English

反应原生的axios post响应未定义

[英]axios post response is undefined for react-native

I have used axios post to fetch the data in react-native but response is undefined. 我使用axios post来获取react-native中的数据,但响应未定义。 Sometimes it cant catch the error but it can be caused by my backend code. 有时它无法捕获错误,但它可能是由我的后端代码引起的。

I tried http and https but this didn't work or it doesnt working with or without header. 我尝试过http和https,但这不起作用,或者它无法使用或不使用标头。 Is there anyone to find the mistake on my code? 有没有人在我的代码上找到错误?

 axios
      .post(
        "http://192.168.1.34:3000/auth/",
        {
          email: email,
          password: password
        },
        {
          headers: { "Content-Type": "application/json" }
        }
      )
      .then(response => {
        console.log(response);
        console.log(email);
      })
      .catch(error => {
        console.log("loginAction:", error);
      });

First thing I'd do is check CORS for your POST request. 我要做的第一件事是检查CORS的POST请求。

Its often an overlooked issue and relatively easy to solve. 它往往是一个被忽视的问题,相对容易解决。 Postman is good about setting correct headers and options. 邮差很好地设置了正确的标题和选项。 If it still doesn't work I would go through Postmans set up one by one and mimic the POST there.. 如果它仍然无法工作,我会逐个设置Postmans并模仿那里的POST。

Use an interceptor to return the response and sometimes you need to use http://10.0.2.2 instead of localhost 使用拦截器返回响应,有时您需要使用http://10.0.2.2而不是localhost

axios.interceptors.response.use((response) => {
    return response;
}, function (error) {
    return Promise.reject(error.response);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM