简体   繁体   English

Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0

[英]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.

相关问题 未捕获(承诺)SyntaxError:JSON 中位置 0 的意外标记 &lt; - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Django : Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - Django : Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 未捕获的 Promise Rejection SyntaxError: Unexpected token u in JSON at position 0 - Uncaught Promise Rejection SyntaxError: Unexpected token u in JSON at position 0 如何修复“Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0”错误 - How to fix “Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0” ERROR Redux 应用程序错误:未捕获(承诺中) SyntaxError:意外令牌 &lt; 在 position 0 处的 JSON - Redux app error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 使用 vuejs - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 using vuejs Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 (React/Redux App) - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 (React/Redux App) React Js: Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 VM101:1 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - VM101:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 VM39:1 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - VM39:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM