【问题标题】:Axios request displaying response on iOS but not working with androidAxios 请求在 iOS 上显示响应但不适用于 android
【发布时间】:2020-02-25 07:27:05
【问题描述】:

我正在使用axios 将一些数据发布到本机反应的服务器,但我遇到了一个问题,我的代码在 IOS 中运行良好,但我对 android 案例几乎没有问题。这是我的代码,看看吧。

login = () => {
    const { username, password } = this.state;
    let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if (username == "") {
      //alert("Please enter Email address");
      this.setState({ email: "Please enter Email address" });
    } else if (reg.test(username) === false) {
      //alert("Email is Not Correct");
      this.setState({ email: "Email is Not Correct" });
      return false;
    } else if (password == "") {
      this.setState({ email: "Please enter password" });
    } else {
      this.openProgressbar();
      axios
        .post(
          CONSTANT.BaseUrl + "user/do-login",
          {
            email: username,
            password: password
          }
        )
        .then(async (response) => {
          if (response.data.type == "success") {
            await AsyncStorage.setItem(
              "full_name",
              response.data.profile.pmeta.full_name
            );
            await AsyncStorage.setItem(
              "user_type",
              response.data.profile.pmeta.user_type
            );
            await AsyncStorage.setItem(
              "profile_img",
              response.data.profile.pmeta.profile_img
            );
            await AsyncStorage.setItem(
              "profileBanner",
              response.data.profile.pmeta.banner_img
            );
            await AsyncStorage.setItem(
              "profileType",
              response.data.type
            );
            await AsyncStorage.setItem(
              "projectUid",
              response.data.profile.umeta.id
            );
            await AsyncStorage.setItem(
              "projectProfileId",
              JSON.stringify(response.data.profile.umeta.profile_id)
            );
            this.setState({ isProgress: false })
            RNRestart.Restart();
          } else if (response.data.type == "error") {
            this.setState({ isProgress: false });
            alert("Please Check Your Email / Password or Check Network ");
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
    Keyboard.dismiss();
  };

在 IOS 情况下,它在响应后移入 .then,但在 ANDROID 中,它移入 .catch

【问题讨论】:

  • 错误在说什么?
  • @AndreiOlar 错误:java.lang.Double 无法在 convertError (AsyncStorage.js:345) at AsyncStorage.js:338 处转换为 java.lang.String 它显示此错误

标签: javascript android ios react-native axios


【解决方案1】:

请从这里更改链接: response.data.profile.umeta.id

对此:JSON.strignify(response.data.profile.umeta.id) 我认为这应该为您完成这项工作。

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 2020-06-13
    • 1970-01-01
    • 2020-04-10
    • 2020-07-19
    • 2019-04-11
    • 2018-06-22
    • 2018-11-21
    • 2021-03-24
    相关资源
    最近更新 更多