首页 > 解决方案 > 获取 SyntaxError: Unexpected token while Running react app

问题描述

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

如何解决这个问题?

标签: reactjsasync-awaitfetch-api

解决方案


result返回 html 而不是 json。run console.log(result.text()),很可能会有错误的描述。


推荐阅读