最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: Unexpected token p in JSON at position 0 In fetch - Stack Overflow

programmeradmin2浏览0评论
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
Add a ment  | 

2 Answers 2

Reset to default 2

try 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

发布评论

评论列表(0)

  1. 暂无评论