0

I got an if-else script:

     $('#favitem').live('click', function () {
        var fid = $(this).parent().attr('id');

        if (isFav(fid)) {
            alert("Do you want to remove from favorite?");
        }
        else {
            alert("Add to favorite?");
        }
    });

calling the isFav script function:

    function isFav(fid) {

    $.ajax({

        url: '/Stock/IsFav',
        type: 'GET',
        data: { id: fid },
        success: function (result) { return result; }
    });
}

which in turn calling my controller action:

    public Boolean IsFav(int id)
    {
        var food = dbEntities.FOODs.Single(f => f.FoodID == id);
        if (food.FavFlag == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Everything seems works fine, I get a true from firebug, BUT i get the alert message from the else statement. The if statement just never being entered. I cant get what is wrong here. Any idea?? Please help..

2 Answers 2

1

The ajax request in isFav is async and the isFav method will return before it is completed.

This is probably how I would solve it:

function isFav(fid, callback) {
    $.ajax({
        url: '/Stock/IsFav',
        type: 'GET',
        data: { id: fid },
        success: function (result) { callback(result); }
    });
}


 $('#favitem').live('click', function () {
    var fid = $(this).parent().attr('id');

    isFav(fid,function(result){
        if(result && result.toLowerCase() == "true"){
            alert("Do you want to remove from favorite?"); 
        } else {
            alert("Add to favorite?");  
        }
    });
});

You want to make sure that the code in the if-block is run after the ajax request is done. In this snippet this is solved by the callback method that is called when the ajax success function is executed.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi.. I appreciate that... but it din work... I get all alert from the If, but never get from the else...
That is because the data being returned is a string and not a boolean. The if only validates that result is not null and therefor always returns true. I have updated my answer to solve this.
Really thanks a lot!! result && result.toLowerCase() == "true" this is the key to make it works, really appreciate your help..thanks!
1

You're not really returning true from within isFav function. Also ajax is asynchornous, so your code (if statement) actually continues to execute until ajax finishes, so at the moment of execution the result of isFav is undefined. So that's why else is being executed.

You're gonna need some remodeling.

function isFav(fid) {

    $.ajax({

        url: '/Stock/IsFav',
        type: 'GET',
        data: { id: fid },
        success: function (result) {
          if(result == 'favorite') alert("Do you want to remove from favorite?");
          else alert("Add to favorite?");
        }
    });
}

 $('#favitem').live('click', function () {
    var fid = $(this).parent().attr('id');
    isFav(fid);
});

3 Comments

I wil get the alert message from the if, i cant enter the else
Yeah well make sure the condition is met. What does the result has to be when item is favorite? Correnct the condition in the form i've edited above.
HI, i had change the condition to true and false, now the else statement is shown, the if is never enter... please help...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.