我正在使用 flickr API 构建图像搜索程序,但遇到了某个错误。“未捕获(承诺)SyntaxError:意外的令牌 < 在 JSON 位置 0 处异步函数(异步)getData @ index.js:19”我无法从 API 获取任何数据。有人可以解释为什么我收到这个错误以及如何解决它?这是 JavaScript 代码const api_key = "123456789ABCDEFGH"; let quantity = "5";const userSearch = document.getElementById("search-field"); // input searchasync function getData() { const URL = `https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=${api_key}&per_page=${quantity}&tags=${encodeURIComponent( userSearch.value )}`; let response = await fetch(URL, { method: "GET" }); let data = await response.json(); return await data; // HERE is data error.
3 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
不起作用,我以前有过,然后删除只是为了尝试如果没有它可以更好地工作。现在当我再次添加时,同样的问题..
如果你的意思是这样:
const URL = `https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=${api_key}&per_page=${quantity}&tags=${encodeURIComponent(
userSearch.value
)}&format=json`;
凤凰求蛊
TA贡献1825条经验 获得超4个赞
我解决了它,如果其他人需要它,我会在这里解释。还要添加:&nojsoncallback=1`
const URL = `https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=${api_key}&per_page=${quantity}&tags=${encodeURIComponent(
userSearch.value
)}&format=json&nojsoncallback=1`;
添加回答
举报
0/150
提交
取消
