1
function check_login(){
    var authenticated  = false;
    $.ajax({type:"POST",url:"/account/islogin/",data:"",async:"false",
        success:function(data){
            if(data=="null"){
                signup();
                authenticated =  false;
            }
            authenticated =  true;   
        }
    });
    return authenticated;
};

why the function always return false,even authenticated=true is executed.

6
  • Null is a primitive type. Dont check for null as a string Commented Jun 12, 2013 at 13:27
  • the success function is called asychronously. check_login() will always return false, because the success function is called later Commented Jun 12, 2013 at 13:28
  • 2
    You are missing a else there Commented Jun 12, 2013 at 13:28
  • 2
    You really should use callbacks or promises instead of making a synchronous call. Commented Jun 12, 2013 at 13:31
  • But if you really want to make a synchronous request (although you shouldn't), you have to set the async option properly. Use a boolean instead of a string: async: false. Commented Jun 12, 2013 at 13:39

2 Answers 2

3

The issue is that you run you if statement, and then set authenticated to true every time. It sounds like you are looking for an if else statement.

function check_login(){
    var authenticated  = false;
    $.ajax({type:"POST",url:"/account/islogin/",data:"",async:false,
        success:function(data){
            if(data=="null"){
                signup();
                authenticated =  false;
            } else {
                authenticated =  true; 
            }
        }
    });
    return authenticated;
};

or even better, you can set up the authenticated to default to true and then only set it to false when the data is null.

function check_login(){
    var authenticated  = false;
    $.ajax({type:"POST",url:"/account/islogin/",data:"",async:false,
        success:function(data){
            authenticated =  true;
            if(data=="null"){
                signup();
                authenticated =  false;
            }
        }
    });
    return authenticated;
};
Sign up to request clarification or add additional context in comments.

5 Comments

I think another issue is that the request is asynchronous. "false" is probably regarded as truthy value.
I think that has more to do with how the ajax call is fired. stackoverflow.com/questions/1478295/…
I don't understand what you are trying to tell me, so I will elaborate: The OP's code will only work if the request is synchronous. That's why they set the async option, but they passed a (non-empty) string as value instead of a boolean. I think we can assume that internally jQuery tests the option simply with if (option.async). Therefore async: "false" will be equivalent to async: true i.e. the request will be asynchronous and the function will always return false.
Ahh, you are correct! I just assumed it was in relation to the async and not the actual value. i will update my answer.
@FelixKling You're right! In the source, the only time they check for the async property, it's if ( !s.async ) { callback(); }, which is right after calling .send() on the XMLHttpRequest. So since it's their way of making sure the success is executed immediately for async requests (and obviously wouldn't run if s.async is truthy
-2

Because the authenticated variable inside your Ajax function and the one inside check_login() are in different scopes. They can't affect one another.

4 Comments

The callback passed to $.ajax is a closure. It has access to authenticated defined in check_login.
I could have sworn I had this exact issue the other day where even though to me it looked like a closure and should therefore work it wouldn't and the two variables couldn't see each other. Of course judging by the two downvotes I must be mistaken.
@MrMisterMan - this would happen if you redeclared the variable with var
@Igor ha! I think that must have been what I was doing #facepalm#. I'll leave this incorrect answer here in case these comments help anyone else out.

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.