Send Ajax request in Javascript


var req = new XMLHttpRequest();
var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
req.open("POST", url, true);

//Send the proper header information along with the request
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

req.onreadystatechange = function() {//Call a function when the state changes.
if(req.readyState == 4 && req.status == 200) {
alert(http.responseText);
}
}
req.send(params);

//or

var req = new XMLHttpRequest();
req.open('GET', url);

req.onload = function() {
// This is called even on 404 etc
// so check the status
if (req.status == 200) {
// Resolve the promise with the response text
console.log(req.data);
}
else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
console.log('error');
}
};

// Handle network errors
req.onerror = function() {
console.log('Network Error');
};

// Make the request
req.send();

//or

var xhr = new XMLHttpRequest();
xhr.open('POST', 'somewhere', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function () {
// do something to response
console.log(this.responseText);
};
xhr.send('user=person&pwd=password&organization=place&requiredkey=key');