我有一些php检查用户是否登录并返回false,然后通过ajax将其传递给浏览器,让用户知道他需要登录.
在FF中工作完美,警报返回'true'但在IE中,无论我做什么,它都会返回'null '
// Check for logged in user
$.getJSON(baseUrl+"index.php/login/checkState", function(data) {
alert(data);
if(data==true){
dologInState();
}else{
dologOutState();
}
});
Run Code Online (Sandbox Code Playgroud)
PS:从PHP返回的字符串完全是假的
有任何想法吗?
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}
Run Code Online (Sandbox Code Playgroud)
...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();
}
});
Run Code Online (Sandbox Code Playgroud)
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).