2
function readMemory(ptr, size)
{
    $.post("readMemory.php", { ptr : ptr, size : size}, function(data)
    {

    });     

    //return "data"
}

Hi, I just want to get data variable as return of readMemory function. Is there any proper way to do this?

Batuhan Tasdoven
  • 798
  • 7
  • 19
  • Since it is an asynchronous call, the `data` is not available yet when the function returns. You can assign the `data` to some variable in the `success` function. – Michael Berkowski Feb 13 '12 at 12:26
  • possible duplicate of [return function value from $.post callback](http://stackoverflow.com/questions/8414371/return-function-value-from-post-callback) – Quentin Feb 13 '12 at 12:30
  • possible duplicate of [JavaScript asynchronous return value / assignment with jQuery](http://stackoverflow.com/questions/7779697/javascript-asynchronous-return-value-assignment-with-jquery) – Felix Kling Feb 13 '12 at 12:50

4 Answers4

3

The improper way would be to make it a synchronous request (which I think would require using the lower level ajax method instead of post).

The proper way would be to forget about returning anything and use your callback function to do whatever you need to do with the data.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

The best thing to do is use a callback:

function readMemory(ptr, size, callback)
{
    $.post("readMemory.php", { ptr : ptr, size : size}, function(data)
    {
        callback(data, ptr, size);
    });     
}

(Note that we have ptr and size being passed to callback as well as data; this is usually good practice.)

So code you were expecting to use readMemory like this:

var data = readMemory(ptr, 100);
doSomethingWith(data);
doSomethingElseWith(data);

...will instead look like this:

readMemory(ptr, 100, function(data) {
    doSomethingWith(data);
    doSomethingElseWith(data);
});

This is because your readMemory function only starts the POST operation; it returns before the operation completes (the operation is asynchronous).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

AJAX is asynchronous. This means that by the time your readMemory function returns, the result might not be available yet.

The only place that you can reliably consume the results of an AJAX call is inside the success callback:

function readMemory(ptr, size)
{
    $.post("readMemory.php", { ptr : ptr, size : size}, function(data)
    {
        // here and only here you can use data
    });     

    // at this stage there is no way to get the data 
}

So the proper way is to not return anything from your function and do whatever you intended to do with those results inside the callback.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0
var tabela;

function doSomethingWith(response) 
{    
 tabela = response;
}

function get_more(id)
{        
    var a =  id + 11;
    var b =  id + 21;

    $.post("../php/empresas/empresas_tab.php",{id:a, _id:b},function(data){
         doSomethingWith(data);
        $("#tabEMP").html(data);
        //console.error(data);
    });
}
joe_young
  • 4,107
  • 2
  • 27
  • 38
José Araújo
  • 69
  • 1
  • 5