【发布时间】:2019-05-14 03:31:41
【问题描述】:
我想和服务器通信。
为了与服务器通信,必须在headers 中添加两项。
注意:写入的键值不是实际的键值。
- api_key: "abcdegeg123456842536ebebeb1yeyju",
- game_key: "abcdegeg123456842536ebebeb1yeyju"
我尝试与之通信的代码:
checkNickName = async () => {
fetch("http://192.168.0.44:11000/v1/point/auth/change_nickname", {
method: "POST",
body: JSON.stringify({
wallet_address: "0f8751828af26816ef996c37e611b945304a6e99",
new_nickname: this.state.nickname
}),
headers: {
// "Content-Type": "application/json"
api_key: "abcdegeg123456842536ebebeb1yeyju",
game_key: "abcdegeg123456842536ebebeb1yeyju"
}
})
.then(res => res.json())
.then(response => {
console.log("response:" + response);
console.log(response.resultCode);
if (response.resultCode == "S000") {
Alert.alert("info","scess");
} else alert(response.result);
})
//console.log("Success:", JSON.stringify(response))
.catch(error => console.error("Error:", error));
};
但这不起作用
错误:,[TypeError:网络请求失败]
如何与服务器通信?还有其他方法吗?
提前谢谢你
【问题讨论】:
-
你是在 iOS 还是 Android 中测试网络通话?
-
@QuokMoon 现在正常了,但是从服务端接收数据时为null。
标签: javascript ajax react-native fetch expo