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.

liuzhijun
  • 4,329
  • 3
  • 23
  • 27

2 Answers2

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;
};
David Ziemann
  • 960
  • 1
  • 8
  • 22
  • 2
    I think another issue is that the request is asynchronous. `"false"` is probably regarded as truthy value. – Felix Kling Jun 12 '13 at 13:31
  • I think that has more to do with how the ajax call is fired. http://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax – David Ziemann Jun 12 '13 at 13:32
  • 3
    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`. – Felix Kling Jun 12 '13 at 13:34
  • 1
    Ahh, you are correct! I just assumed it was in relation to the `async` and not the actual value. i will update my answer. – David Ziemann Jun 12 '13 at 13:39
  • @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 – Ian Jun 12 '13 at 13:52
-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.

punkrockbuddyholly
  • 9,675
  • 7
  • 36
  • 69
  • The callback passed to `$.ajax` is a **closure**. It has access to `authenticated` defined in `check_login`. – Felix Kling Jun 12 '13 at 13:32
  • 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. – punkrockbuddyholly Jun 12 '13 at 13:35
  • 1
    @MrMisterMan - this would happen if you redeclared the variable with `var` – Igor Jun 12 '13 at 13:36
  • @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. – punkrockbuddyholly Jun 12 '13 at 13:38