SyntaxError:意外的标记“<”,“<!DOCTYPE”...不是有效的 JSON

Pau*_*g19 33 node.js reactjs

这是一个PERN应用程序。我不记得曾经遇到过这个错误,并且在进行 Google 搜索时也没有找到任何记录。

我在index.html文件中没有看到任何错误:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <!-- <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> -->
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

它可能从哪里来?我一启动应用程序就得到了它。

小智 34

我只是一个初学者,但是当您将错误的 URL 传递给 fetch 函数时,就会发生这种情况。假设您的db.json文件在端口 8000 上运行,而您正在传递端口 3000。这就是原因。

useFetch("http://localhost:3000/blogs/" + id);
Run Code Online (Sandbox Code Playgroud)

相反,请尝试以下操作。这可能有帮助。:)

useFetch("http://localhost:8000/blogs/" + id);
Run Code Online (Sandbox Code Playgroud)


小智 5

我安装时遇到了同样的错误:npm gh-pages

我的解决方案是删除package.json文件中的主页链接,它起作用了。

  • 通过额外的支持信息可以改进您的答案。请[编辑]添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以[在帮助中心](/help/how-to-answer)找到有关如何写出好的答案的更多信息。 (5认同)