【问题标题】:how can i get the return value of fetch method to be put in an alert?如何获取 fetch 方法的返回值以放入警报中?
【发布时间】:2018-08-28 03:27:25
【问题描述】:

我正在尝试注册,当我提交表单时,应用程序应显示服务返回的警报。这是服务的回报:

{
    "Result": {
        "retVal": "20404|Save Successed!"
    }
}

获取代码如下:

fetch(url, {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ save: 
    { NoKTP: '1234123412341234',
      Nama: 'Rondi',
      Email: 'rondi@rocketmail.com',
      Password: '123qwert',
      NoTelp: '779977',
      NoHP: '081112222345' } 
  })
})
.then( (response) => response.json())
.then( (responseJson) => {
  Alert.alert("respon : " + responseJson.retVal)
})
.catch((error) => {
  console.error(error);
});

但是当我运行应用程序时,警报会显示我 “响应:未定义”

如何让 retVal 值显示在警报中? 有谁能够帮助我?提前谢谢..

【问题讨论】:

    标签: json react-native fetch


    【解决方案1】:

    改变

    Alert.alert("respon : " + responseJson.retVal);
    

    Alert.alert("respon : " + responseJson);
    

    试试

    【讨论】:

      【解决方案2】:

      看起来你需要改变:

      Alert.alert("respon : " + responseJson.retVal)
      

      到:

      Alert.alert("respon : " + responseJson.Result.retVal)
      

      为了匹配您的回复格式:)

      【讨论】:

      • 我尝试更改并添加 Result.. 但它给我一个错误,红色背景显示“未定义不是对象”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多