【问题标题】:Can 't fecth content whit query.ID in nextJS无法在 nextJS 中使用 query.ID 获取内容
【发布时间】:2020-02-01 14:45:10
【问题描述】:

我正在寻找我在这段代码中做错了什么,但你无法解决它。 我需要在 getInitialPops 中使用我的 query.id 来感染一些内容。提取有效,但在我的捕获中,我收到了这个错误:

FetchError: invalid json response body at https://url/resume-true.json reason: Unexpected token < in JSON at position 0

原始端点:

https://url/resume-${query.id}.json

当我 console.log 返回 query.id 时,首先

slug-5 (this is the rigth one)

第二个:

true (I dont'know were this came from)

这是我的 getInitialProps

channelRouter.getInitialProps = async ({ query }) => {
  console.log("query**", query.id);
  try {
    const res = await fetch(
      `https://url/resume-${query.id}.json`
    );
    let data = await res.json();
    return { data };
  } catch (e) {
    console.log(`${e}`);
  }
};

有什么想法吗? 谢谢!!

【问题讨论】:

    标签: javascript reactjs next.js


    【解决方案1】:

    这个错误我已经见过很多次了。

    FetchError: invalid json response body at https://url/resume-true.json reason: Unexpected token &lt; in JSON at position 0

    这意味着您的端点返回的是 HTML 而不是 JSON(因此以 &lt; 开头)

    检查您的端点,它不是有效的 json,在 html 中可能是 404 或 500 状态

    【讨论】:

    • 谢谢彼得,但我已经检查了 json 的格式,它的 okey..
    • 你能用chrome开发者工具-网络流量检查一下吗?
    猜你喜欢
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2014-05-20
    • 2015-10-10
    • 2017-04-29
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多