form serialize javascript (no framework)

后端 未结 23 1393
一生所求
一生所求 2020-11-22 16:07

Wondering is there a function in javascript without jquery or any framework that allows me to serialize the form and access the serialized version?

23条回答
  •  死守一世寂寞
    2020-11-22 17:08

    Here is pure JavaScript approach:

    var form = document.querySelector('form');
    var data = new FormData(form);
    
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
           console.log(this.responseText);
        }
      };
      xhttp.open("POST", "", true);
      xhttp.send(data);
    }
    

提交回复
热议问题