i have ajax call inside my function make it reusable, when my ajax success i want to callback a function,
var ajaxPostCall = function(data , url ,callback){
// Return the $.ajax promise
$.ajax({
data: data,
dataType: 'json',
url: url,
method: 'POST',
beforeSend: function() {
onStartAjaxRequest();
},
success:function(data){
if(typeof callback == "function"){
callback();
}else{
console.log('not callback');
}
},
complete: function (XMLHttpRequest, textStatus) {
onEndAjaxRequest();
}
});
}
var ajaxGetCall = function(data , url ,callback){
// Return the $.ajax promise
$.ajax({
url: url,
dataType: 'json',
method: 'GET',
beforeSend: function() {
onStartAjaxRequest();
},
success:function(data){
//console.log('test');
if(typeof callback == "function"){
callback();
}else{
console.log('not callback');
}
},
complete: function (XMLHttpRequest, textStatus) {
onEndAjaxRequest();
}
});
}
function onStartAjaxRequest(){
$('#spinner').hide();
}
function onEndAjaxRequest(){
$('#spinner').show();
}
$(document).ready(function(){
data = {'name' : 'john'};
function callbackSuccess(){
console.log('success');
}
ajaxPostCall(data , '/proccess.php' , function(){
console.log('success 1');
});
ajaxGetCall(data , '/proccessGet.php?id=12' , function(){
console.log('success 2');
});
})
when i run this code, both of ajax post and get can work.but why only my ajaxget can call the callback 'success2' , the ajaxpost doesnt show 'success1' .. any idea?