Working of callback functions with Ajax
by Mobeen Sarwar
HTML
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<div><h5>Working of Callback functions with Ajax</h5></div>
<div id="api" ></div>
JavaScript
function sendAjax(url, methodType, callback){
$.ajax({
url : url,
method : methodType,
dataType : "json",
success : callback,
error : function (reason, xhr){
console.log("error in processing your request",reason);
}
})
}
// jsonplaceholder fake api to get user details
var URL = "https://jsonplaceholder.typicode.com/users/";
var methodType="GET"
sendAjax(URL,methodType,myCallback );
function myCallback(res){
var test = '<table border="1px"><thead><tr><th>ID</th><th>Name</th><th>Email</th><tr></thead><tbody>';
var tr='';
for(var i=0;i<=9;i++){
tr += '<tr> <td>' + res[i].id + '</td><td>' + res[i].name + '</td><td>' + res[i].email + '</td></tr>';
}
test += tr;
return document.getElementById('api').innerHTML = test;
}