xhr post request javascript code example
Example 1: ajax request
$.ajax({
url : 'more_com.php',
type : 'GET',
data : {variable1 : "some data"},
success : function(result){
},
error : function(result, statut, error){
}
});
Example 2: 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 3: ajax open a request
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "TheFileYouWant.html", true);
xhttp.send();
}