【问题标题】:Value for title can not be cast from ReadablenativeMap to string标题的值不能从 ReadablenativeMap 转换为字符串
【发布时间】:2019-03-30 06:18:18
【问题描述】:
InsertDataToServer = () => {
const { pinValue1 } = this.state;
const { pinValue2 } = this.state;
const { pinValue3 } = this.state;
const { pinValue4 } = this.state;
var String_3 = pinValue1.concat(" " , pinValue2);
var String_4 = String_3.concat(" " ,  pinValue3);
var String_5 = String_4.concat(" " ,  pinValue4);

fetch("http://www.aonde.biz/mobile/doLogin.php", {
  method: "POST",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    "pin":212,

  })
})
  .then(response => response.json())
  .then(responseJson => {
    // Showing response message coming from server after inserting records.
    Alert.alert(responseJson);
  })
  .catch(error => {
    console.error(error);
  });

在上面的代码中,当我传递 pin 参数 API 时会显示此错误。谢谢请告诉我如何解决这个问题。

【问题讨论】:

    标签: android ios reactjs react-native expo


    【解决方案1】:

    您可以简单地从pin 中删除双引号。

    fetch("http://www.aonde.biz/mobile/doLogin.php", {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        pin:212,
      })
    })
    

    【讨论】:

    • 我不明白为什么即使在连接所有字符串之后你也只添加 212 作为你的 pin。但是,当我按照我的建议检查给定的 URL 和正文时,我得到了 status:1 输出。我不明白你为什么说这个问题正在发生。如果你能更详细一点,我可以帮你。
    • 您可以通过获取状态 1 分享您的代码吗?谢谢
    • 我使用邮递员获得了状态 1。但是使用我在此答案中使用的方法,我没有收到您所说的错误,因为您在使用 stringify 时不必将键放在双引号中。只有不是数字时的值。
    【解决方案2】:

    在你的 Alert.alert(responseJson);你需要像这样对它进行字符串化

    Alert.alert(JSON.stringify(responseJson));
    

    请记住,alert 是来自移动应用程序的组件,因此它可能无法理解显示有 json 的代码,如果这不起作用,请使用 responseJson.text()

    【讨论】:

      【解决方案3】:

      当代码从以下位置更改时,此错误已修复:

      Alert.alert('Error:', error)

      到:

      Alert.alert('Error:', error.message)

      【讨论】:

        【解决方案4】:

        Alert 方法中添加标题时错误已修复,因为在文档中调用 Alert 方法时必须给出 Alert 的标题

        【讨论】:

          【解决方案5】:

          确保您的警报显示的是 error.MESSAGE 而不是整个错误。取决于可能是原因的错误及其类型!

          这就是我的问题的原因。

          【讨论】:

          • 当然,这是我的问题。我传递了错误而不是 error.message
          • 是的,我记得我花了好几个小时试图找出问题所在,哈哈。
          【解决方案6】:

          我遇到了这个错误,因为我安装了一个需要我再次编译应用程序的库,而我没有编译它。

          再次编译后一切正常。

          【讨论】:

            【解决方案7】:

            我也遇到过类似的问题。我通过更改其中一种语法来解决它。在你的情况下,试试这个

            fetch("http://www.aonde.biz/mobile/doLogin.php",
              method: "POST",
              header: {
                Accept: "application/json",
                "Content-Type": "application/json"
              },
              body: JSON.stringify({
                "pin":212,
            
              })
            )
            

            我对您的编码所做的更改。我在 URL 后删除了{}

            如果您在 : 上出现红线错误。然后删除 method: header:body:。代码会是这样的

            fetch("http://www.aonde.biz/mobile/doLogin.php",
              "POST",
              {
                Accept: "application/json",
                "Content-Type": "application/json"
              },
              JSON.stringify({
                "pin":212,
            
              })
            )
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-05-07
              • 1970-01-01
              • 1970-01-01
              • 2012-05-19
              • 2018-10-26
              • 2017-11-27
              • 2015-04-21
              • 1970-01-01
              相关资源
              最近更新 更多