admin管理员组文章数量:1428524
const say= document.querySelector(".quotes");
fetch(`/qod.js?category=inspire`)
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
console error
Uncaught (in promise) SyntaxError: Unexpected token p in JSON at position 0 Promise.then (async) (anonymous) @ quotes.js:8
I want to bring the price and print it out.
const say= document.querySelector(".quotes");
fetch(`http://quotes.rest/qod.js?category=inspire`)
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
console error
Uncaught (in promise) SyntaxError: Unexpected token p in JSON at position 0 Promise.then (async) (anonymous) @ quotes.js:8
I want to bring the price and print it out.
Share Improve this question edited Feb 11, 2020 at 21:38 sudo97 9142 gold badges11 silver badges24 bronze badges asked Feb 11, 2020 at 20:00 feelSoWiFfeelSoWiF 411 silver badge4 bronze badges 1- 1 That looks like a JSONP response. en.wikipedia/wiki/JSONP – Daniel W Strimpel Commented Feb 11, 2020 at 20:05
2 Answers
Reset to default 2try to add a content-type and accept to the headers
const say= document.querySelector(".quotes");
fetch(`http://quotes.rest/qod.js?category=inspire`, {
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
}
})
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
https://developer.mozilla/en-US/docs/Web/API/Fetch_API/Using_Fetch
This endpoint is not returning application/JSON, it is returning application/javascript. This looks like its probably used as a part of a JSONP request because it is returning javascript that when executed continues the call chain.
In order to interact with the endpoint you need to either have a library that supports JSONP such as jquery or implement the support yourself. https://en.wikipedia/wiki/JSONP
本文标签: javascriptSyntaxError Unexpected token p in JSON at position 0 In fetchStack Overflow
版权声明:本文标题:javascript - SyntaxError: Unexpected token p in JSON at position 0 In fetch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745529393a2661987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论