【问题标题】:Uncaught (in promise) SyntaxError: Unexpected end of JSON input Promise.then (async)Uncaught (in promise) SyntaxError: Unexpected end of JSON input Promise.then (async)
【发布时间】:2019-08-31 00:10:35
【问题描述】:

我正在尝试将数据添加到我的数据库中。执行我的 fetch 方法后,我收到以下错误

Uncaught (in promise) SyntaxError: Unexpected end of JSON input Promise.then (async)

我已尝试重新配置承诺,但不确定我是否正确执行此操作。

addNewBeer() {
    fetch("https://beer.fluentcloud.com/v1/beer/", {
      body: "{\"name\":\"\",\"likes\":\"\"}",//input beer name and like amount ex "{\"name\":\"Michelob Ultra\",\"likes\":\"-5\"}"
      headers: {
        "Content-Type": "application/json"
      },
      method: "POST"
      })
      .then(response => response.json())
      .then(responseJson => {
        this.setState({
          isLoaded: true,
          dataSource: this.state.dataSource + responseJson,
      });
    })
  }

【问题讨论】:

  • 问题不在于您发布的代码,而在于您收到的回复。只有你可以调试它。

标签: reactjs


【解决方案1】:

当您收到的响应不是 JSON 时,会发生此错误 Uncaught (in promise) SyntaxError: Unexpected end of JSON input Promise.then (async)。错误发生在这部分:

.then(response => response.json())

如果省略该部分,您将看到 res 不是 JSON,可能是字符串。

您可以尝试发送console.log 以查看回复内容,然后在您的问题中提供。

【讨论】:

    猜你喜欢
    • 2018-02-19
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 2019-11-21
    • 2021-08-23
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多