loggingIn()是我的onClick监听器。
loggingIn(){
fetch("http://sampleurl.com", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password,
})
})
.then((response) => response.json())
.then((responseData) => {
console.log(responseData);
if (responseData.response === 1) {
alert("Success");
} else {
alert("Username or password is incorrect");
}
})
.done();
}
有人知道为什么它会给我一个错误吗?当我使用这段代码来反应原生时它工作得很好但是当我将它集成在reactjs中它给了我这个错误
`TypeError: fetch(...).then(...).then(...).done is not a function`
答案 0 :(得分:3)
fetch()返回一个Promise,你可以在其上调用then()来获取响应,并使用catch()来获取抛出的任何错误。
将.done()
替换为.catch((error) => console.error(error))
。
详情请见此处:Networking in React Native
答案 1 :(得分:-1)
该问题是由于缺少catch块而引起的。添加了捕获块后,我的问题解决了。
fetch(URL, config)
.then(response => response.json())
.then((data) => {})
.catch((error) => {
console.log(error);
});