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