【问题标题】:Unexpected end of JSON input in Next.jsNext.js 中的 JSON 输入意外结束
【发布时间】:2021-07-03 08:25:24
【问题描述】:

我正在从 /api/notes/1 获取数据并收到此信息:

{
"id":1,
"author":1,
"title":"Pierwsza notka",
"excerpt":"Taka tam notka, bla bla bla",
"body":"Pirwsza notka elo",
"private":1,
"created_at":"2021-04-07T12:59:59.000Z",
"updated_at":"2021-04-07T12:59:59.000Z"
}

这很好,花花公子,但是当我将它传递给 Next 的 getStaticProps 时:

export async function getStaticProps({ params }) {
  const res = await fetch(`${config.apiURL}/notes/${params.id}`);
  const post = await res.json();

  return { props: { post } };
}

它返回一个错误:

FetchError: invalid json response body at http://localhost:3000/api/notes/1 reason: Unexpected end of JSON input

这是怎么回事?

【问题讨论】:

  • 您是否在浏览器“网络”开发者工具中查看过实际的 HTTP 响应正文是什么样的?
  • 检查您的网络控制台:请求实际上是否正确通过,响应是否是上面的 JSON 对象?
  • 您可以尝试将const post = await res.json(); 切换为const post = await res.text() 和console.logging 帖子吗?
  • @JohanLindskogen 当res.text() post 只是一个空字符串。奇怪的是,在 Chrome Devtools 控制台中运行它时提取很顺利。 @Pointy @Chloe_Anderson 另外,请求不在网络选项卡中。

标签: javascript json reactjs next.js


【解决方案1】:

问题是我的错。提取返回 401 Unauthorized with no body。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-10
    • 2015-08-05
    • 2017-12-15
    • 2019-06-29
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    相关资源
    最近更新 更多