我有 2 个功能: 1 检查用户是否登录,如果他登录了 - 它需要第二个函数来获取他的用户 ID。现在我只是用警报测试它,看看我是否得到了参数。
这是第一个功能:
//Checking if the user is logged in or not
$(function(){
$.getJSON("inc/API.php", {command : "getUserName"},
function(result){
if(result==null){
$("#divGreeting").html("Hello, guest!");
$("#divLogin").hide();
$("#divUserOption").hide();
$("#divConnectOption").show();
}
else {
alert(getUserID(result));
$("#divGreeting").html("Hello, "+result+"!");
$("#divHeader").html("Hello, "+result+"! <a href='javascript:logout()'>Logout</a>");
$("#divUserOption").html("Hello, "+result+"! <a href='javascript:logout()'>Logout</a>");
$("#divConnectOption").hide();
$("#divLogin").hide();
$("#divUserOption").fadeIn(300);
}
});
});
这是第二个函数,应该返回用户 ID:
function getUserID(){
$.getJSON("inc/API.php",
{
command : "getUserID"
},
function(result){
alert(result);
return result;
});
}
第alert
一个函数的 是undefined
,而第二个函数的 alert 确实有 userID。为什么我不能将它的值返回给第一个函数?为什么我会得到`未定义?
谢谢!`