繁体   English   中英

fetch api 发出 get 请求而不是 post

[英]fetch api makes get request instead of post

我在后端使用 ReactJS 和 nodeJS。

我正在以这种方式处理提交表单:

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
  };

但不知何故,当sendHandler function 被执行时,获取请求而不是发布。 PS 我正在从 localhost 向 https 网站发出请求。

对下面的代码进行快速测试肯定会发送一个 POST(然后会出现 CORS 错误)。 您的控制台中是否出现任何错误?

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM