Send POST data using XMLHttpRequest

前端 未结 13 1508
说谎
说谎 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条回答
  •  Happy的楠姐
    2020-11-21 05:00

    Try to use json object instead of formdata. below is the code working for me. formdata doesnot work for me either, hence I came up with this solution.

    var jdata = new Object();
    jdata.level = levelVal; // level is key and levelVal is value
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "http://MyURL", true);
    xhttp.setRequestHeader('Content-Type', 'application/json');
    xhttp.send(JSON.stringify(jdata));
    
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          console.log(this.responseText);
        }
    }
    

提交回复
热议问题