Juw
Juw

Reputation: 2089

JQuery, ajax logic issue

I am having some problems with my code. And i thought i would share my problem with the great minds on this website to see if someone can give my brain some pointers on how to solve it.

I am working on an app (JQuery, PhoneGap) and i have a login function. The Login function sends login credentials to a webservice. So i am sending it using JQUery ajax. The webservice also contains a fetchData function that let´s my client to get some data. The webservice will check to see if i am currently logged in and that my session hasn´t timed out. If it has timeout i need to login again. And then try again. This is something the user will never see. loginUser() is a function that is used many other times in my code. So don´t want to fiddle around with it to much so i have to make alot of changes.

So here is some psuedo code:

function loginUser()
{
$.ajax(
{
    ....
    success: function(data,status)
    {
        //everything worked great
        return true;
    },
    error: function(data,status)
    {
        //display some user error stuff.
        return false;
    },
});  
}

function getData()
{
$.ajax(
{
    ...
    success: function(data,status)
    {
        //everything worked great. No need to login user again.
    },
    error: function(data,status)
    {
        //Opps user needs to login again and then we need to try again.
    //If we have tried to getData after we tried to login (and got succeess from the loginUser() function)....report error.
        //Below code will not work. But it will show you roughly what i am trying to do. It will also create an infinite loop.
        if (loginUser())
        {
            getData();
        }
    },
});
}

Upvotes: 0

Views: 325

Answers (3)

Gung Foo
Gung Foo

Reputation: 13558

you can make a non-async call and have it's success/error handler set a local variable at your function (since ajax will return only after the request has finised)

function loginUser()
{
  var result = false;
  $.ajax(
  {
    ....
    async: false,
    success: function(data,status)
    {
        //everything worked great
        result = true;
    },
    error: function(data,status)
    {
        //display some user error stuff.
        result = false;
    },
  });  
  return result;
}

Upvotes: 0

Pluda
Pluda

Reputation: 1477

    var user_id, has_valid_session;
    function check_session() {
        $.ajax({
        type: "POST",
        url: "login.php",
        data: "{user:user_id}",
        success: function(result) {
            // you send to your server the user_id and make sure he has a valid session
            if(result == "true") {
                has_valid_session = true;
                continue();
            } else {
                $('#error').html("you need to login first");
                login_user();
            }
        }
    });
}

If I understand your question, you need to load a serverside script php/asp and wait for the result, in my example the result would be true ot false. If true, just continue else show user the login form.

Upvotes: 0

Alnitak
Alnitak

Reputation: 339947

An async function cannot (usefully) return a value.

First, change loginUser so that it returns the jqXHR object:

function loginUser() {
    return $.ajax(...);
}

this should make no difference to (valid) existing uses of the code, which currently returns nothing (actually undefined).

Your second error function can then also register an interest in the status of the loginUser AJAX call:

error: function() {
    var loginPromise = loginUser();
    loginPromise.done(getData);
}

Upvotes: 2

Related Questions