繁体   English   中英

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

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

我使用axios post来获取react-native中的数据,但响应未定义。 有时它无法捕获错误,但它可能是由我的后端代码引起的。

我尝试过http和https,但这不起作用,或者它无法使用或不使用标头。 有没有人在我的代码上找到错误?

 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);
      });

我要做的第一件事是检查CORS的POST请求。

它往往是一个被忽视的问题,相对容易解决。 邮差很好地设置了正确的标题和选项。 如果它仍然无法工作,我会逐个设置Postmans并模仿那里的POST。

使用拦截器返回响应,有时您需要使用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