【发布时间】:2019-03-26 02:38:28
【问题描述】:
我正在构建一个反应原生应用程序并收到以下错误。我想将输入的消息、电子邮件和名称发送到 API,但在 API 中没有显示任何结果。
代码如下:
fetch('localserverusingIPaddress', {
method: 'POST',
headers: {
"Content-Type": "application/json",
'Accept': 'application/json',
},
body: JSON.stringify({
name: this.state.name,
email: this.state.email,
message: this.state.message,
}),
})
.then((response)=> {console.warn(response.json())})
//{
// if (response.status){
// return response.json();
// }
// console.warn(response.json())
// return response.json();
//})
//console.warn(response);
//response.json()
//console.warn(JSON.parse(response))})
.then((responseData)=>{
this.showAlert();
console.warn(responseData);
return responseData;
})
.catch((error) => {
console.warn(error);
});
但是,当我尝试在 iOS 中检查输入的文本时 模拟器,它显示了价值。当我使用邮递员直接将数据发布到 API 时,它也会显示值。所以我开始认为 body 未能传递给 API。
谁能告诉我为什么会发生这种情况以及如何解决这个问题?非常感谢,我已经面临这个问题好几个星期了......
【问题讨论】:
-
.then块或catch块中的日志消息是什么? -
第一行代码中的
localserverusingIPaddress只是这里的占位符吗?运行此命令时,控制台中实际登录的内容是什么? -
@mostafizrahman 第一个
.then给我这个消息{"_40":0,"_65":0,"_55":null,"_72":null},而第二个.then给我undefined消息。catch块中的错误没有显示任何内容.. -
@UzairA。是的,它只是一个占位符,基本上我使用我的 IP 地址作为获取 url,而不是 localhost。
标签: react-native post ios-simulator fetch expo