Jospehione
Jospehione

Reputation: 27

I am getting function undefined error in jQuery, even though the function is defined

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?

Upvotes: 0

Views: 104

Answers (1)

Mulan
Mulan

Reputation: 135197

You need to return the promise

function getMySession() {
  return $.ajax({ ...

Upvotes: 2

Related Questions