gpt4 book ai didi

json - React-Native Fetch "POST"请求在 Android 中抛出 "SyntaxError: Unexpected end of JSON input"

转载 作者:行者123 更新时间:2023-12-03 20:34:02 27 4
gpt4 key购买 nike

这是我的功能
不知道问题出在哪里

fetchData() {
fetch(REQUEST_URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
Request : 'menu',
active : '1',
})
}).then((response) => response.json())
.then((responseData) => {
this.setState({
menu: responseData.Response,
});
})
.catch((error) => {
console.warn('error',error);
})
.done();
}

请指出功能上的问题

最佳答案

发生错误是因为您的响应无法转换为 JSON 格式。出现此问题的原因可能是错误的 header 、响应正文或基于您的服务器的其他各种原因。要走的路 - 因为显然响应不一致 - 是在尝试将响应转换为 JSON 之前执行服务器响应的额外验证。

您可以通过替换来做到这一点:

.then((response) => response.json())



.then((response) => {
// In this case, we check the content-type of the response
if (response.headers.get('content-type').match(/application\/json/)) {
return response.json();
}
return response;
// You can also try "return response.text();"
})

关于json - React-Native Fetch "POST"请求在 Android 中抛出 "SyntaxError: Unexpected end of JSON input",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37184550/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com