首页 > 解决方案 > 如何修复“错误”;获取 api 中的 json 无效

问题描述

我正在使用 fetch api 将数据发布到托管在 heroku 上的 API。我收到错误消息 {error:Invalid JSON}

我该怎么办。

const signUpBtn2 = document.getElementById("enter2");
signUpBtn2.addEventListener("click", function(e){
    e.preventDefault()

    const formData = new FormData()
    const fetchData = {
      method: "POST",
      headers: {
        Accept: "application/json, text/plain, */*",
        "Content-type": "application/json; charset=UTF-8"
      },
      body: formData
    };



    fetch("https://polityco.herokuapp.com/auth/signup", fetchData)
      .then(function(res) {
        return res.json();
      })
      .then(function(text) {
        console.log([text]);
      })
      .catch(function(error) {
        console.log(error);
      });
});

我期望 json 响应,但我得到的响应是 [{…}] 0: {error: "Invalid JSON"} length: 1

标签: javascriptjquerynode.jsjsonfetch

解决方案


推荐阅读