Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0
Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0
我制作了一个小香草 javascript 代码来获取 api 但它抛出错误“未捕获(承诺)语法错误:JSON 中的意外标记 N 在位置 0 ”。我无法理解错误被捕获的确切原因。谁能帮我解决这个问题。
const config = {
url: "https://randomuser.me/",
numberCards: 24
};
fetch(`${config.url}&amount=${config.numberCards}`)
.then(function(response) {
return response.json();
})
.then(function(apiResponse) {
// Output API response to console to view.
console.log(apiResponse);
});
就是因为这个
const config = {
url: "https://randomuser.me/",
numberCards: 24
};
fetch(`${config.url}&amount=${config.numberCards}`)
应该是,
const config = {
url: "https://randomuser.me/api/",
numberCards: 24
};
fetch(`${config.url}?amount=${config.numberCards}`)
因为json数据来自“https://randomuser.me/api/”。不是“https://randomuser.me/”。并且查询字符串必须以“?”开头标记。 “&”标记用于分隔查询字符串。 (像这样“https://example.com/?amount=24&hi=en”)
我制作了一个小香草 javascript 代码来获取 api 但它抛出错误“未捕获(承诺)语法错误:JSON 中的意外标记 N 在位置 0 ”。我无法理解错误被捕获的确切原因。谁能帮我解决这个问题。
const config = {
url: "https://randomuser.me/",
numberCards: 24
};
fetch(`${config.url}&amount=${config.numberCards}`)
.then(function(response) {
return response.json();
})
.then(function(apiResponse) {
// Output API response to console to view.
console.log(apiResponse);
});
就是因为这个
const config = {
url: "https://randomuser.me/",
numberCards: 24
};
fetch(`${config.url}&amount=${config.numberCards}`)
应该是,
const config = {
url: "https://randomuser.me/api/",
numberCards: 24
};
fetch(`${config.url}?amount=${config.numberCards}`)
因为json数据来自“https://randomuser.me/api/”。不是“https://randomuser.me/”。并且查询字符串必须以“?”开头标记。 “&”标记用于分隔查询字符串。 (像这样“https://example.com/?amount=24&hi=en”)