这应该很简单。但即使在互联网上搜索之后,我尝试编写的代码也无法正常工作。
所以我有这个。检查用户是否在线或离线的代码。
//Check if it's a valid user
function checkUser(){
status = (function(){
var isOnline = null;
$.ajax({
async: false,
global: false,
type: "POST",
url: "./system/checkUser.php",
data: $("#webcam").serialize(),
success: function(data){
isOnline = (data == "1");
}
});
return isOnline;
})();
return status;
}
这就是我尝试使用它的方式
function loadProfile(){
var text = checkUser();
alert(text);
if(text != true){
alert("Invalid user!");
return;
}else{
//Code
}
但是无论我从 checkUser() 得到什么,我总是得到“无效用户!”警报,即使文本是真实的。有人能帮我一下吗?我已经无话可说了。
最佳答案
As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().
http://api.jquery.com/jquery.ajax/
<小时/>你能做的是:
function checkUser(callback){
$.ajax({
global: false,
type: "POST",
url: "./system/checkUser.php",
data: $("#webcam").serialize(),
success: callback
})
}
function loadProfile(){
checkUser(function(text){
alert(text);
if(text != true){
alert("Invalid user!");
return;
}else{
//Code
}
});
}
关于JavaScript 忽略 true 和 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21641659/