0

This is what I have in my code to get the session info from the controller via ajax.

function getMySession() {
    $.ajax({
        url: 'ajax-get-actual-session',
        type: 'POST',
        dataType: 'json',
        success: function(response) {
            return response;
        }
    });
}

getMySession()
.done(function(r) {
    if (r) {
        alert(JSON.stringify(r));
    } else {
        alert('bad');
    }
})
.fail(function(x) {
    alert('even worse');
});

But I am getting in Firefox console an error:

TypeError: getMySession(...) is undefined

However, I have already defined it in my code. Why is that? What am I doing wrong?

Any idea, how can i make this code work?

Jospehione
  • 27
  • 4
  • Where did you define it then? – Bram Vanroy Sep 12 '14 at 19:34
  • 2
    you arent returning the promise in your `getMySession` function, the error message is saying your return is undefined not that the function is – Patrick Evans Sep 12 '14 at 19:35
  • `function getMySession() { $.ajax({ url: 'ajax-get-actual-session', type: 'POST', dataType: 'json', success: function(response) { return response; } }); }` – Jospehione Sep 12 '14 at 19:35
  • @PatrickEvans `return response;` in success – Jospehione Sep 12 '14 at 19:36
  • @Jospehione: That `return response;` doesn't actually do anything. – gen_Eric Sep 12 '14 at 19:37
  • @PatrickEvans Should I duplicate `return response;` like this: `function getMySession() { $.ajax({ url: 'ajax-get-actual-session', type: 'POST', dataType: 'json', success: function(response) { return response; } }); return response; }` ? I don't get what do you mean. – Jospehione Sep 12 '14 at 19:37
  • @Jospehione That's not returning anything from `getMySession()`. The success function is run asynchronously when the AJAX call completes. – Barmar Sep 12 '14 at 19:38
  • You need `return $.ajax(` – Patrick Evans Sep 12 '14 at 19:39
  • @Barmar What do you mean? I can alert the session inside the getMySession() function. But I need to 'transfer' this string to another part of my code. Please, try to be more beginnerish, if possible. – Jospehione Sep 12 '14 at 19:40
  • Both the success.response and $.ajax need to return. return success.response to $.ajax and return $.ajax to your function call (getMySession). If there is no global variable set, you gotta return something to get data (in most cases). – below9k Sep 12 '14 at 19:41
  • @Jospehione When you call an asynchronous function it returns immediately, it doesn't wait for its work to be done. `$.ajax` starts the AJAX request, but returns before the server responds. So it can't return the response to `getMySession`, because the response hasn't been received yet. The success function is called in the background later, when the response comes. – Barmar Sep 12 '14 at 19:48
  • I've reopened the question because it does look like you were going in the right direction, using a promise with `.done()`. You simply left out a bit of the syntax, which is shown in @naomik's answer. – Barmar Sep 12 '14 at 19:49

1 Answers1

2

You need to return the promise

function getMySession() {
  return $.ajax({ ...
Mulan
  • 129,518
  • 31
  • 228
  • 259