我正在使用 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 响应状态等,但是,实际上没有任何内容被添加到数据库中。
如果我使用发布的相同数据并在 postman 中调用相同的 API 地址,它工作正常并且数据被添加到数据库中,所以我相当有信心它不是 API 本身,更多的是与以上代码。
最佳答案
可能,您需要返回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));
}
关于javascript - React Isomorphic Fetch 实际上没有发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55059583/