【问题标题】:Getting SyntaxError: Unexpected token while Running react app获取 SyntaxError: Unexpected token while Running react app
【发布时间】:2021-12-10 03:19:43
【问题描述】:

在运行 react 应用程序时,console.log 出现错误

这是我的应用useEffect

useEffect(() => {
    const fetchData = async () => {
      const result = await fetch(`/api/foodies/${name}`);
      const body = await result.json();
      console.log(body);
      setFoodieInfo(body);
    };
    fetchData();
  }, [name]);

我的错误是:uncaught (in promise) syntaxerror: unexpected token < in json at position 0

如何解决这个问题?

【问题讨论】:

  • 响应的格式是什么?看起来“结果”不是 JSON。
  • 它的 json 格式
  • 是你自己的api吗?如果是公共api,你能提供它的url吗?我仍然认为最后有错字或尾随逗号或类似的东西。或者,也许你正在返回 html,所以是一个 '
  • 是的,它是我自己的 api,我使用 express js 创建它。
  • 我在package.json也设置了http://localhost:8000服务器代理

标签: reactjs async-await fetch-api


【解决方案1】:

result 返回 html 而不是 json。运行console.log(result.text()),很可能会有错误说明。

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 2018-09-11
    • 2013-02-25
    • 2022-12-01
    • 2020-01-05
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多