[英]Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0
I have made a small vanilla javascript code to fetch api but it is throwing the error "Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0".我制作了一个小的香草javascript代码来获取api,但它抛出错误“未捕获(承诺)SyntaxError:JSON中位置0处的意外标记N”。 I m not able to understand, exactly why the error is caught.
我无法理解,究竟为什么会捕捉到错误。 Can somebody help me to resolve this.
有人可以帮我解决这个问题。
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);
});
It's because of this.正因为如此。
const config = {
url: "https://randomuser.me/",
numberCards: 24
};
fetch(`${config.url}&amount=${config.numberCards}`)
It should be,它应该是,
const config = {
url: "https://randomuser.me/api/",
numberCards: 24
};
fetch(`${config.url}?amount=${config.numberCards}`)
It's because the json data are from "https://randomuser.me/api/".这是因为 json 数据来自“https://randomuser.me/api/”。 Not "https://randomuser.me/".
不是“https://randomuser.me/”。 And the query strings must begin with a "?"
并且查询字符串必须以“?”开头。 mark.
标记。 "&" mark is used to separate query strings.
“&”标记用于分隔查询字符串。 (like this "https://example.com/?amount=24&hi=en")
(像这样“https://example.com/?amount=24&hi=en”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.