VM1661:1 未捕获(承诺中)语法错误:JSON 中位置 0 处出现意外标记

Zee*_*der 2 javascript express reactjs

大家好,我的网站出现错误很长时间了,我在整个互联网上搜索了答案,但没有找到任何解决方案,这是我的onsubmit代码

onSubmit = () => {
    fetch("http://localhost:2000/signin", {
      method: "post",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        email: this.state.signinEmail,
        password: this.state.signinPassword,
      }),
    })
      .then((response) => response.json())
      .then(console.log(this.state.signinEmail, this.state.signinPassword))
      .then((data) => console.log(data));
  };
Run Code Online (Sandbox Code Playgroud)

我还检查了网络选项卡的响应,它显示成功,但收到此错误不知道如何消除它。我还检查了 Stackoverflow 的解决方案,它写了Accept:application/json但仍然不起作用,但它给了我“错误的请求”错误后端代码是:

app.post("/signin", (req, res) => {
  if (
    req.body.email === database.users[0].email &&
    req.body.password === database.users[0].password
  ) {
    res.send("success");
  } else {
    res.status(400).json("error logging in");
  }
});
 
Run Code Online (Sandbox Code Playgroud)

我还通过Postman对其进行了测试,它可以成功运行,没有错误。这是 json 服务器。 在此输入图像描述

小智 10

当您向服务器发出请求并将响应解析为 JSON,但它\xe2\x80\x99s 不是 JSON 时,就会发生这种情况。

\n
fetch('/url').then(res => res.json())\n
Run Code Online (Sandbox Code Playgroud)\n

实际的请求效果很好。它得到了回应。但这res.json()就是失败的。

\n

根本原因是服务器返回了 HTML 或其他一些非 JSON 字符串。

\n

您可以尝试更改res.json()res.text().

\n
onSubmit = () => {\n    fetch("http://localhost:2000/signin", {\n      method: "post",\n      headers: {\n        "Content-Type": "application/json",\n      },\n      body: JSON.stringify({\n        email: this.state.signinEmail,\n        password: this.state.signinPassword,\n      }),\n    })\n      .then((response) => response.text())\n      .then((data) => console.log(data));\n  };\n
Run Code Online (Sandbox Code Playgroud)\n