gpt4 book ai didi

javascript - 语法错误: Unexpected token p in JSON at position 0 In fetch

转载 作者:行者123 更新时间:2023-12-01 00:09:13 25 4
gpt4 key购买 nike

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

image

我想带上价格并打印出来。

最佳答案

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

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

关于javascript - 语法错误: Unexpected token p in JSON at position 0 In fetch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60176478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com