0

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?

image

Ferry Sanjaya
  • 224
  • 2
  • 17

1 Answers1

0

well, i just find out myself and i know the problem...my proccess.php on ajaxpost is not returning json object properly , meanwhile i put dataType: 'json' in my ajaxpost, thats why my ajaxpost is not going to success callback

but i still wonder on my ajaxget still going to success callback even my /proccessGet.php?id=12 does not return json object , is ajax with GETmethod ignoring datatype:json?

Ferry Sanjaya
  • 224
  • 2
  • 17