Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript VM1661:1未捕获(承诺中)语法错误:JSON中位置0处出现意外的标记s_Javascript_Reactjs_Express - Fatal编程技术网

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

Javascript VM1661:1未捕获(承诺中)语法错误:JSON中位置0处出现意外的标记s,javascript,reactjs,express,Javascript,Reactjs,Express,大家好,我的网站有一个错误已经很长时间了,我在整个互联网上搜索了答案,但没有找到任何解决方案。这是我的提交代码 onSubmit = () => { fetch("http://localhost:2000/signin", { method: "post", headers: { "Content-Type": "application/json",

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

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));
  };
我还检查了网络标签的响应,它说成功,但得到这个错误不知道如何摆脱它。我还检查了问题的解决方案 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");
  }
});
 
我还通过邮递员对它进行了测试,它在上面运行成功,没有错误。 这就是json服务器。

当您向服务器发出请求并将响应解析为JSON(但不是JSON)时,就会发生这种情况

fetch('/url').then(res => res.json())
实际的请求很有效。它得到了回应。但是失败的是
res.json()

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

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


您的响应(
res.send(“success”);
)不是JSON,因此不要尝试通过
将其作为JSON读取。然后((response)=>response.JSON())
。要将正文理解为文本,请使用
.text()
。由于这是一个非常具体的错误,将来不太可能对其他人有用,因此我建议删除该问题。快乐编码!
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.text())
      .then((data) => console.log(data));
  };