我正在使用flickr API构建图像程序搜索,并且陷入了特定错误。
“未捕获(在承诺中)SyntaxError:JSON中位置0处的意外令牌
我无法从API获取任何数据。有人可以解释为什么我得到此错误以及如何解决它吗?
这里是JavaScript代码
const api_key = "123456789ABCDEFGH";
let quantity = "5";
const userSearch = document.getElementById("search-field"); // input search
async 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.
将format=json
参数添加到您的网址中。默认情况下,此端点以xml格式返回结果
不起作用,我以前有过,所以删除了它只是想尝试一下是否可以更好地工作。现在,当我再次添加时,同样的问题..
如果您的意思是这样:
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`;