当我尝试在 React js 中运行 ajax 调用时,出现错误,fetch errorSyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
在这里我已经进行了 ajax 调用,任何人都可以在我的代码中查看它并帮助我解决这个问题吗?
fetch('https://randomuser.me/api/?results=5', {
method: 'GET',
mode: "no-cors",
headers: {
"Accept": "application/json",
'Content-Type': 'application/json'
}
})
.then(response => { return response.json(); })
.then(responseData => { return responseData; })
.catch(err => {
console.log("fetch error" + err);
});
如果是 get 请求,则不需要标头和模式;
fetch('https://randomuser.me/api/?results=5')
.then(response => { return response.json(); })
.then(responseData => { console.log( responseData ) })
.catch(err => {
console.log("fetch error" + err);
});
这是因为mode: "no-cors"
.因为包括它给了你空的 html 响应。评论或删除该行,您就可以开始了。没有cors选项使其仅卡在HTTP选项方法上。