【问题标题】:NextJS - Unexpected token < in JSON at position 0NextJS - JSON 中位于位置 0 的意外标记 <
【发布时间】:2021-01-28 14:14:31
【问题描述】:

我遵循了有关 NextJS 的教程,现在尝试对其进行一些修改。我想在页面中包含 data.json 文件。但我总是收到错误消息“意外的令牌

我知道我应该将 JSON 文件转换为字符串(?)。我该怎么做?

  let res = await fetch(`${server}/data`);
  const articles = await res.json();

  return {
    props: {
      articles,
    },
  };
}

我的 GIT-Repo 在这里:https://gitlab.com/pauernet/nextjs-test/

【问题讨论】:

    标签: json reactjs next.js


    【解决方案1】:

    您的问题是 data.json 文件不在公用文件夹中。您必须将其从根文件夹移到那里。 domain.tld/file 只能访问公用文件夹中的文件。您还必须指定完整的文件名:

    let res = await fetch(`${server}/data.json`);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-19
      • 2016-12-21
      • 2019-08-30
      • 2018-04-29
      • 2018-03-02
      • 2020-10-31
      • 2021-10-22
      相关资源
      最近更新 更多