Reputation: 613
This should really be simple. But the code I'm trying to write is just not working, even after searing around the internet.
So I have this. code to check if a user is online or offline.
//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;
}
and this is how I try to use it
function loadProfile(){
var text = checkUser();
alert(text);
if(text != true){
alert("Invalid user!");
return;
}else{
//Code
}
But whatever I get from checkUser(), I always get the "Invalid user!" alert, even when text is true. Can anyone help me here? I'm lost for words.
Upvotes: 0
Views: 101
Reputation: 5476
async: false
is deprecated so you have to use a callback to access data from an ajax request.
You could change your checkUser
function so that it takes a success callback as a parameter.
I personally don't like using big callbacks, so I usually get around the problem with a $.Deferred:
function checkUser() {
status = $.Deferred();
$.ajax({
global: false,
type: "POST",
url: "./system/checkUser.php",
data: $("#webcam").serialize(),
success: function(data) {
status.resolve(data == "1");
}
});
return status;
}
function loadProfile() {
var textD = checkUser();
textD.done(function (text) {
alert(text);
if(text != true){
alert("Invalid user!");
}
});
}
Upvotes: 1
Reputation: 94409
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/
What you can do is:
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
}
});
}
Upvotes: 3