Dee*_*eee 1 javascript reactjs react-native
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();
}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么它给我一个错误?当我使用这段代码来反应本机时,它工作得很好,但是当我将它集成到reactjs中时,它给了我这个错误
`TypeError: fetch(...).then(...).then(...).done is not a function`
Run Code Online (Sandbox Code Playgroud)
小智 5
fetch() 返回一个 Promise,您可以调用 then() 来获取响应,并调用 catch() 来获取抛出的任何错误。
替换.done()为类似的东西.catch((error) => console.error(error))。
有关详细信息,请参阅此处:React Native 中的网络
| 归档时间: |
|
| 查看次数: |
10828 次 |
| 最近记录: |