【问题标题】:axios post response is undefined for react-native对于 react-native,axios 后响应未定义
【发布时间】:2019-10-12 00:50:30
【问题描述】:

我使用 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);
      });

【问题讨论】:

  • 你对后端有什么期望?
  • 只想返回具有指定电子邮件和密码的用户令牌。邮递员完全可以工作。

标签: javascript react-native axios


【解决方案1】:

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

这通常是一个被忽视的问题,并且相对容易解决。 Postman 擅长设置正确的标题和选项。如果它仍然不起作用,我会通过 Postmans 一个一个地设置并在那里模仿 POST ..

【讨论】:

    【解决方案2】:

    使用拦截器返回响应,有时您需要使用http://10.0.2.2 而不是 localhost

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

    【讨论】:

      猜你喜欢
      • 2021-05-26
      • 1970-01-01
      • 2020-11-23
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多