vanilla JavaScript
and calls the callback function
if the third parameter's type is function
function ajax(url, methodType, callback){
var xhr = new XMLHttpRequest();
xhr.open(methodType, url, true);
xhr.send();
xhr.onreadystatechange = function(){
if (xhr.readyState === 4 && xhr.status === 200){
if (typeof callback === "function") {
callback(xhr.responseText);
}
}
}
}
Example of calling the above methodajax(url, 'GET', function(resp) {
console.log(resp);
})