How can I call a function when a XHRHttpRequest is completed or returned response seccuss. Without using jquery.
xmlhttp = new XMLHttpRequest();
xmlhttp.success()
this kind of a function which will be called after every request
How can I call a function when a XHRHttpRequest is completed or returned response seccuss. Without using jquery.
xmlhttp = new XMLHttpRequest();
xmlhttp.success()
this kind of a function which will be called after every request
You can use the following code.
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari, Chrome
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
console.log('something went wrong. Check the error! '+ e);
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var response = ajaxRequest.responseText;
// do something here with the response object
// you could JSON.parse to parse the response if it is json string
}
}
ajaxRequest.open("GET", "get_data.php", true); // true for asynchronous request
ajaxRequest.send(null); // initiate a send(call)
xmlhttp.onreadystatechange = function(){
if(request.status===200){
var response = xmlhttp.responseText;
//do something with response
}
}