我已经使用koa服务器和postgresql数据库为我的应用程序创建了一个简单的api,并使用postman和浏览器对其进行了测试,两者均能正常工作。现在,我试图在我的反应本机应用程序中获取数据(在实际设备上进行测试),但它会返回以下响应:
{"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "1bbc1ba3-a1b4-442e-ba85-d09d5d54f4e6", "offset": 0, "size": 9}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "1bbc1ba3-a1b4-442e-ba85-d09d5d54f4e6", "offset": 0, "size": 9}}, "headers": {"map": {"content-length": "9", "content-type": "text/plain; charset=utf-8", "date":
"Mon, 20 Apr 2020 12:26:12 GMT"}}, "ok": false, "status": 404, "statusText": undefined, "type": "default", "url":
"https://a1cd97c8.ngrok.io/posts/,function%20dispatchAction()%20%7B%20%20%20%20[native%20code]%7D"}
我正在使用ngrok URL映射,因为如果没有https,我的请求将被直接拒绝。
这是我的请求代码:
const searchURL = React.useState('https://a1cd97c8.ngrok.io/posts/');
const fetchPosts = async () => {
console.log('fetching');
fetch(searchURL, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
.then((response) => {
console.log('response: ', response);
return response.json();
})
.then((json) => console.log('TESTOUTPUT', json))
.catch((err) => console.log('error: ', err.message));
};
我在catch块中收到的错误消息是:
error: JSON Parse error: Unexpected identifier "Not"
我在做什么错了?
答案 0 :(得分:2)
useState
钩子返回一个数组,因此您应该解构结果
const [searchURL, setSearchURL] = React.useState('https://a1cd97c8.ngrok.io/posts/');