React Isomorphic Fetch 实际上没有发布

React Isomorphic Fetch not actually posting

我正在使用 React 创建网站,其中一个步骤涉及创建事件。我创建了一个使用同构获取发布到 API 的步骤。

import fetch from "isomorphic-fetch";

export function createEvent(data) {
    console.log(data);
    return fetch("whatson/createEvent.php", {
        method: "POST",
        body: JSON.stringify(data),
        headers: {
            "Content-Type": "application/json"
        },
        redirect: "follow"
    }).then(response => {
        if (response.status >= 200 && response.status < 300) {
            alert("Success");
            console.log(response);
            return response;
        } else {
            console.log("Something went wrong");
            console.log(response);
        }
    });
}

我已经完成了响应,它说它是 A-OK,200 响应状态等,但是,实际上没有任何内容被添加到数据库中。

如果我使用发布的相同数据并在邮递员中调用相同的 API 地址,它工作正常并且数据被添加到数据库中,所以我相当有信心它不是 API 本身和上面的代码有更多的关系。

可能,您需要 return response.json():

import fetch from "isomorphic-fetch";

export function createEvent(data) {
  console.log(data);
  return fetch("whatson/createEvent.php", {
    method: "POST",
    body: JSON.stringify(data),
    headers: {
      "Content-Type": "application/json"
    },
    redirect: "follow"
  }).then(response => {
    if (response.status >= 200 && response.status < 300) {
      alert("Success");
      return response.json();
    } else {
      console.log("Something went wrong");
      console.log(response);
    }
  }).then(json => console.log(json));
}