Javascript function AJAX call returns undefined
<p>When the AJAX call is made, the onsuccess doesn't return back the string builder data, but undefined instead.</p>

<pre><code>var MyApp = window.MyApp || {};

MyApp.Data = function() {
    var temp = 'Test',
    ShowMethod = function() {
        $.ajax({
            url: "some url",
            type: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: ShowMethodSucceeded,
            error: FailedMethod
        });
    },
    ShowMethodSucceeded = function(data) {
        var results = data.d.results;
        if(results.length &gt; 0) {
            temp = '';
            for(var i = 0; i &lt; results.length; i++) {
               temp += 'string builder stuff';
            }
        } else {
            alert('no records');
        }
        return temp;
    },
    FailedMethod = function() {
        alert('error');
    };
    return {
        Show: ShowMethod
    };
}();

$(document).ready(function() {
   $('#divResults').append(MyApp.Data.Show());
});
</code></pre>

<p>Also, here are a couple of different versions of the code do not reap the correct result.</p>

<pre><code>MyApp.Data = function() {
    var temp = 'Test',
    ShowMethod = function() {
        $.ajax({
            url: "some url",
            type: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: ShowMethodSucceeded,
            error: FailedMethod
        });
    },
    return temp;
    // returns 'Test' instead of the string builder

MyApp.Data = function() {
    var temp = 'Test',
    ShowMethod = function() {
        $.ajax({
            url: "some url",
            type: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: function(data) { temp = data; },
            error: FailedMethod
        });
    },
    return temp;
    // returns 'Test' instead of the JSON from data
</code></pre>

<p><strong>How do I return the data from the success function of the AJAX call?</strong></p>

<p>By the time the AJAX call is made, the function is finished executing and the return variable is not populated yet.  The UI is being built dynamically, and I want to use the string builder function to return the string to append the functions that dynamically builds the UI.</p>

<p>Thank you in advance.</p>