[英]fetch api makes get request instead of post
i am using ReactJS and nodeJS on backend.我在后端使用 ReactJS 和 nodeJS。
I'm handling submitting of form this way:我正在以这种方式处理提交表单:
const [email, setEmail] = useState({
name: "",
subject: "",
message: "",
address: "",
});
const sendHandler = async e => {
e.preventDefault();
setSending(true);
const res = await fetch("/api/send-mail", {
method: "post",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(email),
});
...rest of the code
};
But somehow, when sendHandler
function is executed, get request is made instead of post.但不知何故,当sendHandler
function 被执行时,获取请求而不是发布。 PS i'm making request from localhost to https website. PS 我正在从 localhost 向 https 网站发出请求。
A quick test of your code below definitely sends a POST (which then gets a CORS error).对下面的代码进行快速测试肯定会发送一个 POST(然后会出现 CORS 错误)。 Are you getting any errors in your console?您的控制台中是否出现任何错误?
const email = { name: "", subject: "", message: "", address: "", }; const sendHandler = async e => { const res = await fetch("http://www.mocky.io/v2/5ec1a76e2f00006600c34d9b", { method: "post", headers: { "Content-Type": "application/json", }, body: JSON.stringify(email), }); console.log('Rest of code'); }; sendHandler(email);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.