0

I recover data from a Requette in ajax. I want to use this data in a function and return it but when I call the function it returns nothing

function getdata(nom_produit) {

    $.ajax({
        type: "POST",
        url: "traitement/panne/test_panne.php",
        data: {
            nom_produit: nom_produit
        },

        success: function(data) {
            var obj = jQuery.parseJSON(data);

            jQuery.each(obj["resultat"], function(index, value) {

            })
        }
    });
    return obj;
}

how i can return the data?

S4beR
  • 1,872
  • 1
  • 17
  • 33
  • 1
    you can't return from like this. `$.ajax` is an asynchronous call so when result is called `success` function is still pending execution – S4beR Apr 03 '16 at 14:06

1 Answers1

0

you can't return from like this. $.ajax is an asynchronous call so when return is called your success function is still pending execution. you can do something like below though to achieve same result.

function getdata(nom_produit, callback) {

    $.ajax({
        type: "POST",
        url: "traitement/panne/test_panne.php",
        data: {
            nom_produit: nom_produit
        },

        success: callback
    });
}

and from the place you are calling this function you can do something like below

var successFunction = function(data) {
    hideOverlay(); // hide overlay once response is there

    // your code to process data and show in UI 
}

showOverlay(); // code to show loading image in UI till response comes
getData(someId, successFunction);
S4beR
  • 1,872
  • 1
  • 17
  • 33