📌  相关文章
📜  提交表单而不重定向 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:13.458000             🧑  作者: Mango

代码示例1
function formSubmit(event) {
   var url = "/post/url/here";
   var request = new XMLHttpRequest();
   request.open('POST', url, true);
   request.onload = function() { // request successful
      // we can use server response to our request now
      console.log(request.responseText);
   };

   request.onerror = function() {
      // request failed
   };

   request.send(new FormData(event.target)); // create FormData from form that triggered event
   event.preventDefault();
}

// and you can attach form submit event like this for example
function attachFormSubmitEvent(formId) {
   document.getElementById(formId).addEventListener("submit", formSubmit);
}