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);
Here is what the above code is Doing:
1. Create a new XMLHttpRequest object
2. Create a variable to hold the data we want to send to the server
3. Open a connection to the server
4. Set the request header to tell the server what kind of data we’re sending
5. Create a function to handle the response from the server
6. Send the data to the server