how to handle xhr request code example
Example 1: xhr post send
var xhr = new XMLHttpRequest();
var params = 'field1='+postfield1+'&field2='+postfield2;
xhr.open('POST', 'http://exmaple.com', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if(xhr.status == 200) {
alert(this.responseText);
}
}
xhr.send(params);
Example 2: xmlhttprequest javascript
function reqListener () {
console.log(this.responseText);
}
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("GET", "http://www.example.org/example.txt");
oReq.send();