What XMLHttpRequest method must be used to send a POST request to the server? code example
Example 1: xmlhttp js post request
var xhr = new XMLHttpRequest();
xhr.open("POST", '/url', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
// Request finished. Do processing here.
}
}
xhr.send("name=Hello&id=world");
Example 2: xhr post send
var xhr = new XMLHttpRequest();
var params = 'field1='+postfield1+'&field2='+postfield2;
xhr.open('POST', 'http://exmaple.com', true);
//Send the proper header information along with the request
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function() {//Call a function when the state changes.
if(xhr.status == 200) {
alert(this.responseText);
}
}
xhr.send(params);
Example 3: What XMLHttpRequest method must be used to send a POST request to the server?
var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);