tags:

views:

51

answers:

2

I have some php that checks if a user is logged in and returns false, this is then passed via ajax to the browser to let the user know he needs to log in.

Works perfect in FF and the alert returns 'true' but in IE, it returns 'null' no matter what I do

// Check for logged in user
    $.getJSON(baseUrl+"index.php/login/checkState", function(data) {
                    alert(data);
        if(data==true){
            dologInState();
        }else{
            dologOutState();
        }
    });

PS: the string returned from PHP is simply false

Any ideas?

+7  A: 

If your JSON is valid (see json.org and JSONlint), data will be an object or an array, not a flag.

The string false, on its own, is not valid JSON. The top level in JSON notation is always an object ({ ... }) or an array ([ ... ]). To make it valid JSON, you might do this:

{"success": false}

...and then test

$.getJSON(baseUrl+"index.php/login/checkState", function(data) {
    alert(data);
    if(data.success){   // <== Note here we're checking the `success` property of the deserialized object
        dologInState();
    }else{
        dologOutState();
    }
});

Some "JSON" parsers are in fact full JavaScript expression parsers (basically just using eval, although this is dangerous), which may interpret the string false in the way you intend, but you can't rely on that (because a correct parser will reject it).

T.J. Crowder
Hi, thanks 4 your help. I gave it a try and also thought that was the problem but Im still getting an error in ie: 'success' is null or not an object? fireBug shows the string returned from php as: {"success":false}
Derrick
@Derrick: Are you checking `success` or `data.success`? The callback from `getJSON` receives the deserialized object. If the JSON string is `{"success": false}`, the deserialized object in `data` has one property, `success`, with the value `false`. I'll update the answer with more context.
T.J. Crowder
@T.J.: +1, only a small note: Yes, objects are *truthy*, but in the equality comparison e.g. `x == y`, if `y` is `Boolean`, `x == ToNumber(y)` will be compared, so the comparisons `({}) == true` and `({}) == false` actually both evaluate to `false`. Other different case occurs with empty array objects, e.g. `[] == false; // true`, because an empty array coerce to zero when converted to number: `+[] === 0`.
CMS
I used if(data.success){...} however doing the following gives me TRUE:$.each(data,function(i,v){alert(v);})
Derrick
@CMS: Thanks, I clearly need to double-check those rules. (I *never* use `== <boolean>` in my own code, defensive habits from C and Java days [and I don't find them very readable].) :-)
T.J. Crowder
Thanks for the help guys, I guess Ill have to loop thru a single array to find the value in IE. Got to love IE hey.
Derrick
@Derrick: TRUE in all caps? That would be *very* weird. If your PHP is really returning an application/json HTTP response with the text `{"success": false}` (suggest viewing the raw HTTP response, with Firebug on Firefox or Dev Tools on Chrome, etc.), I can guarantee you the `getJSON` callback will give you an object with a `success` property for the `data` argument. I'd take another look at the actual response generated by PHP.
T.J. Crowder
@Derrick: *"I guess Ill have to loop thru a single array to find the value in IE. Got to love IE hey."* This is not an IE thing. I use JSON responses with IE all the time, and they don't get mangled. This is something about the response coming back to the browser from your PHP script.
T.J. Crowder
@T.J ... yeah Im not fond of messing my code up with an unnecessary loop. Also I'm using php's json_encode function to do the conversion.
Derrick
A: 

I eventually managed to sort this out. The problem was not the ajax call to begin with.

It was that the ajax calls were getting its true and false values from php doing a simple "is the authentication cookie present" check.

I cant tell you how or why this was working in FF and acting up in IE and it could have been something I did - but - since 90% of the application is built out of js and ajax calls the solution was to handle all cookie checks with Java script and not with PHP at all.

Ive even got the cookie to store json strings now, its beautiful.

Thanks for all the help guys, much appreciated.

Derrick