返回

使用 Fetch 和 Check Status OK 发布表单

发布时间:2022-05-11 18:57:08 275
# php# 数据

使用 fetch 和 vanilla JS 发布表单数据时如何获得响应代码,我有这个代码发布但不使用 response.ok 检查响应代码

 <form method="post" id="form1" class="myForm" action="includes/vehicles/add-veh.php">
                <input type="hidden" name="userid" id="userid" value="4448" />
                <input type="hidden" name="vehid" id="vehid" value="84488" />
                <button type="submit" name="submit" ><i class="fa-solid fa-circle-plus"></i> Save To Your List</button>
                </form>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
        document.querySelector('.myForm').addEventListener('submit', function (event) {
    var data = this;
    fetch(data.getAttribute('action'), {
      method: data.getAttribute('method'),
      body: new FormData(data)
    })
            .then(res=>res.text())
      .then(function (data) {
        
     })
          .then((res) => {
    if (response.ok) { 
            alert('success');
        }
        };
    
    event.preventDefault();

  });
});
    </script>
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像