【发布时间】:2017-11-22 15:32:20
【问题描述】:
我希望这里有人可以帮助我解决这个问题。我已经与这个错误作斗争太久了。
我正在向传感器的 API 发出获取请求,这始终导致相同的错误:JSON Parse error: Unexpected EOF
我尝试在网上找到答案并尝试了很多建议,但都没有奏效。
在开发环境中,我通过本地网络访问 API。
fetchUsers(timer) {
let data = {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Bearer ' + this.state.token.access_token
}
}
fetch('http://' + this.state.ip + ':5000/api/Devices/031.01.0657/LiveState', data)
.then(response => response.json())
.then(responseJson => {
this.setState({
isLoading: false,
error: false,
userDataSource: responseJson,
refreshing: false,
time: timer
}, function () {
// If success, do something (I never make it to this part)
});
})
.catch(error => {
this.setState({
isLoading: false,
error: true
})
console.error(error);
});
}
我当然一直在使用其他方式来查看 API 返回的内容:
我在使用 API 接口时得到响应的 JSON:
{
"device": null,
"patient": null
}
我在使用 Postman 客户端时收到的 JSON 响应:
{
"device": null,
"patient": null
}
使用与代码中完全相同的 URL。返回的 JSON 也作为有效 JSON 传递。
【问题讨论】:
标签: json react-native fetch eof