3

Possible Duplicate:
How to return the response from an AJAX call from a function?

on my App namespace i've just defined a function:

version 1

window.App = {
  isLogged: function () {
    $.get('/user/isLogged', function (data) {
      if (data == 'true') {
        return true;
      }
      return false;
    });
  }
};

version 2

window.App = {
  isLogged: function () {
    var test = $.get('/user/isLogged');
    console.log(test.responseText);
  }
};

On version 1 when i try the function on firebug 'App.isLogged()' i got a nice undefined :S

On version 2 when i try the function on firebug, the responseText seems to be undefined :stuck:

I'm pretty new about javascript, and maybe a scope issue...

The goal of my function is clear i think, there's a better way to achieve this?

Community
  • 1
  • 1
cl0udw4lk3r
  • 2,663
  • 5
  • 26
  • 46
  • looks like `/user/isLogged` doesn't exist. It may come from the first `/` which means root directory. – Christopher Chiche Jan 19 '13 at 13:20
  • By the way, you can also do `return data=='true'` in version 1. – Christopher Chiche Jan 19 '13 at 13:22
  • @ChrisJamesC: No, the problem is that Ajax does not work this way. You cannot return values like that from an Ajax callback (well, of course it could also be that `/user/isLogged` does not exist, but that's a different issue). – Felix Kling Jan 19 '13 at 13:22
  • 5
    AJAX is asynchronous. Very important to understand what that means. WIll find many explanations in a search – charlietfl Jan 19 '13 at 13:22
  • Use firebug network tab to see the real result of the request. It will tell you the status code (e.g 404 not found, 500 internal server error, 200 ok) and response if applicable. – Paul Fleming Jan 19 '13 at 13:23
  • @FelixKling thanks for the reminder about the asynchronous behavior. – Christopher Chiche Jan 19 '13 at 13:24
  • 1
    Your `App.isLogged` does not returned anything. All `return` in your #1 option is just return from the inner handler function of `.get()`, not return from the `App` function. – Hieu Le Jan 19 '13 at 13:27
  • Wow, dat instant comments! Btw the response from the server is good: 200 - "true", btw i'll see the @Felix Kling link first... – cl0udw4lk3r Jan 19 '13 at 13:34

1 Answers1

2

on first version $.get is asynchronous that's why you don't get a return value

on second version $.get returns deferred object that doesn't have responseText field

window.App = {
  isLogged: function () {
    var dfd = $.Deferred();
    $.get('/user/isLogged', function (data) {
      if (data == 'true') {
        return dfd.resolve();
      }
      return dfd.reject();
    });
    return dfd.promise();
  }
};

$.when(App.isLogged()).then(function() {
  //your code
}).fail(function() {
  //fail code
});
salexch
  • 2,644
  • 1
  • 20
  • 17
  • 1
    You don't have to create your own deferred object, you just return what `$.get` returns. – Felix Kling Jan 19 '13 at 13:48
  • @Felix Kling sure you right.. there are many code versions that could work.. – salexch Jan 19 '13 at 13:50
  • Oh wait... if you are doing the test inside the success callback, then it makes sense to use your own. Sorry, ignore my first comment. – Felix Kling Jan 19 '13 at 13:51
  • @Felix Kling :) removing the edit.. – salexch Jan 19 '13 at 13:55
  • mh, yes i think this is the answer for my "version 2", the version 1 is well answered by @FelixKling on [link](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call-from-a-function) +1 for Felix and answer for @salexch! – cl0udw4lk3r Jan 19 '13 at 19:01