小编lon*_*mar的帖子

使用Fetch API邮寄表单数据

我正在尝试使用Fetch API从表单中检索数据并将其邮寄,但是我收到的电子邮件为空。响应似乎成功,但未发送任何数据。我究竟做错了什么?

这是我的JS代码和我的php / html代码段(如果相关)

(function() {

  const submitBtn = document.querySelector('#submit');

  submitBtn.addEventListener('click', postData);

  function postData(e) {
    e.preventDefault();

    const first_name = document.querySelector('#name').value;
    const email = document.querySelector('#email').value;
    const message = document.querySelector('#msg').value;

    fetch('process.php', {
        method: 'POST',
        body: JSON.stringify({first_name:first_name, email:email, message:message})
    }).then(function (response) {
      console.log(response);
      return response.json();
    }).then(function(data) {
      console.log(data);
      // Success
    });

  }

})();



<!-- begin snippet: js hide: false console: true babel: false -->
Run Code Online (Sandbox Code Playgroud)
<?php 
    $to = "example@mail.com"; 
    $first_name = $_POST['first_name'];
    $from = $_POST['email']; 
    $message = $_POST['message'];
    $subject = "Test Email";
    $message …
Run Code Online (Sandbox Code Playgroud)

javascript php ajax fetch fetch-api

0
推荐指数
1
解决办法
2380
查看次数

标签 统计

ajax ×1

fetch ×1

fetch-api ×1

javascript ×1

php ×1