从Ajax请求返回数据[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call" dir="ltr">How do I return the response from an asynchronous call?</a>
                            <span class="question-originals-answer-count">
                                (38 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2016-04-03 14:18:16Z" class="relativetime">4 years ago</span>.</div>
        </div>
    </aside>

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?

</div>

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);