-2

Possible Duplicate:
How can I return a value from an AJAX request?

I wrote function for taking username from .php file

function pobierzLogin()
{
  var myVar= "0";
  $.post("logowanie.php", { "logowanie" : 3 }, function(odp)
  {
    myVar = odp;
  });
  return myVar;
}

myVar will return 0, but alert(odp); shows good result.

Community
  • 1
  • 1
ThisGuy
  • 5
  • 1
  • I'm guessing the function is returning `myVar` before it gets assigned to `odp`. You can use `$.ajax` and `complete`.http://api.jquery.com/jQuery.ajax/ – elclanrs Oct 06 '12 at 20:10

4 Answers4

2

Ajax is ASYNCHRONOUS. So your function returns variable before assigning value to it. Do it like:

function pobierzLogin(callback)
{
  var myVar= "0";
  $.post("logowanie.php", { "logowanie" : 3 }, function(odp)
  {
    callback(odp);
  });
}

Then use it like this:

pobierzLogin(function(odp){
      //do smthing with odp
});

Edit:

You can set async: false in ajax parameters, in this case ajax call will be synchronous:

return $.ajax({
    url : "logowanie.php",
    type : "post",
    dataType: "json", 
    async: false,
    data: {
        "logowanie": 3
    }
})
karaxuna
  • 26,752
  • 13
  • 82
  • 117
2

This should work just fine.

function pobierzLogin()
{
    var myVar= "0";
    return $.post("logowanie.php", { "logowanie" : 3 }, function(odp)
    {
        return odp;
    });
}

I have used this from time to time and had it work successfully every time.

UPDATE

With help of @user1689607 this should be

function pobierzLogin()
{
    return $.ajax({
        url : "logowanie.php",
        type : "post",
        dataType: "json", 
        data: {
            "logowanie": 3
        }
    })
}

pobierzLogin().done(function(odp){
   //... do stuff
});
brenjt
  • 15,997
  • 13
  • 77
  • 118
  • What do you expect will be returned from `pobierzLogin()`? – I Hate Lazy Oct 06 '12 at 20:20
  • `odp`... that was returned from the `$.post` callback. – brenjt Oct 06 '12 at 20:21
  • That's incorrect. `$.post` is asynchronous, and will never return the return value of its callback. – I Hate Lazy Oct 06 '12 at 20:22
  • If you expect that the value given to the callback will be returned from `pobierzLogin()`, then my explanation is that you've never had it work. It could be made to work, but only by taking advantage of the fact that `$.post` returns a Deferred object. – I Hate Lazy Oct 06 '12 at 20:26
  • @user1689607 You are right! I just double checked my code and since it was just returning true or false it made it seem as thought it was working. I feel like such a noob – brenjt Oct 06 '12 at 20:33
  • That's alright. Thing is that you've almost got a working solution. If you get rid of the callback, but continue to return the `jqxhr` object that `$.post` returns, you can chain `.done(function(odp) { alert(odp); });` on the end, and it'll work. – I Hate Lazy Oct 06 '12 at 20:39
  • ...[here's a demo](http://jsfiddle.net/Nk8vD/) – I Hate Lazy Oct 06 '12 at 20:40
  • 1
    Perfect, Well you always learn something new. Thank you! – brenjt Oct 06 '12 at 20:42
0

It's because your request happens asynchronously, so the value doesn't get set till after pobierzLogin method completes, since the request takes time to go out to the internets and return.

Instead of doing what you are doing, define some other method that you want to fire when the request returns, and execute that in your callback to $.post.

var whenRequestReturns = function(args){ ...  }

$.post("logowanie.php", { "logowanie" : 3 }, function(odp)
  {
    whenRequestReturns(); // invoke here
  });

or just do something like

$.post("logowanie.php", { "logowanie" : 3 }, whenRequestReturns)

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

Your alert(odp); must be inside callback of post as $.post is asynchronous.

Modified code:

function pobierzLogin(cb)
{
  var myVar= "0";
  $.post("logowanie.php", { "logowanie" : 3 }, function(odp)
  {
     myVar = odp;
      cb(odp):
   });
   return myVar;
 }
 pobierzLogin(function(myVar ){
   alert(myVar );
});
Anoop
  • 23,044
  • 10
  • 62
  • 76