繁体   English   中英

语法错误:JSON 中位置 0 处的意外标记 p 在提取中

[英]SyntaxError: Unexpected token p in JSON at position 0 In fetch

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));
});

控制台错误

Uncaught (in promise) SyntaxError: Unexpected token p in JSON at position 0 Promise.then (async) (anonymous) @quotes.js:8

图片

我想把价格拿来打印出来。

尝试添加内容类型并接受标题

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.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

此端点不返回应用程序/JSON,它返回应用程序/javascript。 这看起来可能用作 JSONP 请求的一部分,因为它返回 javascript,执行时会继续调用链。

为了与端点交互,您需要有一个支持 JSONP 的库,例如 jquery,或者自己实现支持。 https://en.wikipedia.org/wiki/JSONP

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM