Send POST data using XMLHttpRequest

前端 未结 13 1474
说谎
说谎 2020-11-21 04:27

I\'d like to send some data using an XMLHttpRequest in JavaScript.

Say I have the following form in HTML:

<         


        
13条回答
  •  鱼传尺愫
    2020-11-21 05:03

    This helped me as I wanted to use only xmlHttpRequest and post an object as form data:

    function sendData(data) {
      var XHR = new XMLHttpRequest();
      var FD  = new FormData();
    
      // Push our data into our FormData object
      for(name in data) {
        FD.append(name, data[name]);
      }
    
      // Set up our request
      XHR.open('POST', 'https://example.com/cors.php');
    
      // Send our FormData object; HTTP headers are set automatically
      XHR.send(FD);
    }
    

    https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript

提交回复
热议问题