React Native fetch returns error: JSON Unexpected EOF
React Native fetch returns error: JSON Unexpected EOF
我希望这里有人可以帮助我解决这个问题。我一直在与这个错误作斗争太久了。
我正在向传感器的 API 发出获取请求,这始终导致相同的错误:JSON 解析错误:意外的 EOF
我试图在网上找到答案并尝试了很多建议,但 none 都奏效了。
在开发环境中,我正在通过本地网络访问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 returns:
我在使用API接口时得到的JSON响应:
{
"device": null,
"patient": null
}
我在使用 Postman 客户端时收到的 JSON 响应:
{
"device": null,
"patient": null
}
使用与代码中完全相同的 URL。返回的 JSON 也作为有效 JSON.
通过
我想它在这一行失败了:response.json().
您可以在尝试转换之前查看响应的内容:
.then(response => {
console.log(JSON.stringify(response, null, 4))
return response.json())
}
我不确定
null
您的 JSON 中的值。我认为你应该使用
"null"
希望对您有所帮助!
只需捕获正在抛出的行中的错误,以便它。问题是 response.json 为 null
try {
responseData = await response.json();
} catch (e) {
dispatch({ type: XXX_SSSSS, service: null });
}
我在使用 fetch 上传图片时遇到了同样的问题,它在调试时工作正常,但是当我发布时出现了同样的错误
尝试安装 npm install react-native-url-polyfill
并在 app.js
中导入“react-native-url-polyfill/auto”
我希望这里有人可以帮助我解决这个问题。我一直在与这个错误作斗争太久了。
我正在向传感器的 API 发出获取请求,这始终导致相同的错误:JSON 解析错误:意外的 EOF
我试图在网上找到答案并尝试了很多建议,但 none 都奏效了。
在开发环境中,我正在通过本地网络访问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 returns:
我在使用API接口时得到的JSON响应:
{
"device": null,
"patient": null
}
我在使用 Postman 客户端时收到的 JSON 响应:
{
"device": null,
"patient": null
}
使用与代码中完全相同的 URL。返回的 JSON 也作为有效 JSON.
通过我想它在这一行失败了:response.json().
您可以在尝试转换之前查看响应的内容:
.then(response => {
console.log(JSON.stringify(response, null, 4))
return response.json())
}
我不确定
null
您的 JSON 中的值。我认为你应该使用
"null"
希望对您有所帮助!
只需捕获正在抛出的行中的错误,以便它。问题是 response.json 为 null
try {
responseData = await response.json();
} catch (e) {
dispatch({ type: XXX_SSSSS, service: null });
}
我在使用 fetch 上传图片时遇到了同样的问题,它在调试时工作正常,但是当我发布时出现了同样的错误 尝试安装 npm install react-native-url-polyfill 并在 app.js
中导入“react-native-url-polyfill/auto”