Skip to content Skip to sidebar Skip to footer

Jquery Ajax Call, Return Value Problem

function getMore(from){ var initData = '&start-index='; initData += from; $.ajax({ type:'POST', url: '', //removed the URL data: initData, dataType: 'json', succes

Solution 1:

functionFuncionCallGetMore(){
    //...    
    getMore('x-value', FuncionGetReturn);
    //...
}  

functionFuncionGetReturn(error, value){
   if (!error) {
       // work value
   }
}

functiongetMore(from, fn){

  var initData = "&start-index=" + from;

  $.ajax({
    type:"POST",
    url: '', //removed the URL
    data: initData,
    dataType: 'json',
    success: function(result) {
      fn(false, result);
    },
    error: function(errorThrown) {
      fn(true);
    }


    });

    return;
}

Solution 2:

The only way that you can do what you're describing is to make the AJAX call synchronous, which you don't want to do since it will lock the UI thread while the request is being made, and the browser may will to freeze. No one likes freezing.

What you want to do is use callbacks. Post the code of the other functions involved so I can get a better idea of what is going on. But basically, what you want to do is to create an asynchronous loop.

functionlistBuilder() {
    var onError = function(response) {
        // ...
    };
    var onSuccess = function(response) {
        // Handle the items returned here// There are more items to be had, get them and repeatif ( response.length == 250 ) {
            getMore(onSuccess, onError, 250);
        }
    };

    getInitialSet(onSuccess, onError);
}

functiongetMore(onSuccess, onError, from) {
    $.ajax({
        type:"POST",
        url: '', //removed the URLdata: "&start-index=" + from,
        dataType: 'json',
        success: onSuccess,
        error: onError
    });
}

Post a Comment for "Jquery Ajax Call, Return Value Problem"