[英]Javascript scope and Ajax call doesn't work correctly
I have some problems. 我有一些问题。
First of all, my php file shoutbox.php, it's used for to log in an user. 首先,我的php文件shoutbox.php,用于登录用户。
session_start();
if(! isset($_SESSION["loggedIn"])) {
include "login.html";
}
else {
if(isset($_POST["submit"]) && $_POST["submit"] == "Log In") {
if(! isset($_POST["username"]) || $_POST["username"] == "" || ! isset($_POST["password"]) || $_POST["password"] == "") {
$GLOBALS["logErr"] = "Please, filln all fields!";
echo json_encode(false);
}
else if(isset($_POST["username"]) && $_POST["username"] != "" && isset($_POST["password"]) && $_POST["password"] != "") {
if(isset($_POST["select"]) && $_POST["select"] != "") {
session_start();
$_SESSION["loggedIn"] = TRUE;
$_SESSION["username"] = $_POST["username"];
$_SESSION["password"] = $_POST["password"];
$_SESSION["type"] = $_POST["select"];
$_SESSION["ip"] = get_ip_address(); /* Return current ip */
$user = array(
"username" => $_SESSION["username"],
"islogged" => $_SESSION["loggedIn"],
"password" => $_SESSION["password"],
"ip" => $_SESSION["ip"],
"type" => $_SESSION["type"]
);
echo json_encode($user);
}
}
}
else if(isset($_POST["logout"]) && $_POST["logout"] == "Log Out") {
session_start();
unset($_SESSION["loggedIn"]);
unset($_SESSION["username"]);
unset($_SESSION["password"]);
unset($_SESSION["type"]);
unset($_SESSION["ip"]);
echo json_encode(false);
}
if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] == TRUE) {
echo json_encode(true); /* user is logged in */
}
else {
echo json_encode(false); /* user is not logged in */
}
}
It returns false or true and if is true it returns user array encoded. 它返回false或true,如果为true,则返回用户数组编码。
This is the Ajax call, I have to call 2 responses, false or true and if response is true, also user array.. 这是Ajax调用,我必须调用2个响应,即false或true,如果响应为true,则还要调用用户数组。
function sb_UserIsLoggedIn() {
$.ajax({
url: 'shoutbox.php',
type: 'POST',
dataType: 'json',
success: function(response, user) {
if(response) {
function user(user) {
CurrUserInfo = {
u_name: user["username"],
u_psw: user["password"],
ip: user["ip"],
typelog: user["type"],
logged: user["islogged"]
};
if(CurrUserInfo.typelog == "anonimous") {
OnlineListObj.onlineList.anonimous.push(user["username"]);
}
else if(CurrUserInfo.typelog == "visible") {
OnlineListObj.onlineList.visible.push(user["username"]);
}
OnlineListObj["onlineList"]["total"] = OnlineListObj.onlineList.anonimous.length + OnlineListObj.onlineList.visible.length;
OnlineListObj["onlineList"]["phrase"] = lang["there_are"] + OnlineListObj.onlineList.total + lang["online"] + OnlineListObj.onlineList.anonimous.length + lang["anonimous"] + OnlineListObj.onlineList.visible + lang["visibles"];
LoggedIn = true;
window.alert(LoggedIn);
window.alert(OnlineListObj["onlineList"]["total"]);
window.alert(OnlineListObj["onlineList"]["phrase"]);
window.alert(CurrUserInfo.ip);
window.alert(CurrUserInfo.typelog);
window.alert(CurrUserInfo.logged);
window.alert(CurrUserInfo.u_name);
window.alert(CurrUserInfo.u_psw);
}
}
else {
window.location.href = "shoutbox.php";
}
},
error: function(response) {
sb_Error("Unknown error, try again");
console.log(lang["ajax_error"]);
console.log(response.responseText);
LoggedIn = false;
},
});
}
Alerts say undefined for these: 警报说这些没有定义:
window.alert(CurrUserInfo.ip);
window.alert(CurrUserInfo.typelog);
window.alert(CurrUserInfo.logged);
window.alert(CurrUserInfo.u_name);
window.alert(CurrUserInfo.u_psw);
This because the call to user array fails...but a call works correctly, it returns true or false.. Why does it work and the other (to user array) not? 这是因为对用户数组的调用失败了……但是一个调用可以正常工作,返回true或false。为什么它起作用,而另一个(对用户数组)不起作用? I'd like ajax call true or false and user array.
我想ajax调用true或false和用户数组。
Other problem: 其他问题:
In code about sb_isUserLoggedIn() I defined a var, LoggedIn... 在有关sb_isUserLoggedIn()的代码中,我定义了一个变量LoggedIn ...
But if you call LoggedIn in another function, returns undefined: 但是,如果您在另一个函数中调用LoggedIn,则返回undefined:
/* Checks if the user is online */
function sb_isOn() {
sb_UserIsLoggedIn();
if(LoggedIn != false) {
return true;
}
else {
return false;
}
}
As I written, this code returns an error, LoggedIn is undefined. 如我所写,此代码返回错误,LoggedIn未定义。
How can I resolve all these problems? 我该如何解决所有这些问题?
Thanks in advance. 提前致谢。
It looks to me that the PHP makes two calls to echo json_encode()
when it is successful. 在我看来,PHP成功执行了两次调用以
echo json_encode()
。 One to echo the user array and one to echo true
. 一种回应用户数组,另一种回应
true
。 I think that results in invalid JSON. 我认为这会导致JSON无效。
I suggest you write the PHP so it makes one and ONLY one call to echo json_encode()
. 我建议您编写PHP,以便它仅使一次调用
echo json_encode()
。 You could have it return the following for success: 您可以让它返回以下成功信息:
echo json_encode(array("success" => true, "user" => $user));
And the following for failure: 以及以下失败信息:
echo json_encode(array("success" => false, "message" => 'Please, fill in all fields!'));
Then in the Javacript, you can have something like the following: 然后,在Javacript中,您可以看到以下内容:
success: function(result) {
if (result.success) {
var user = result.user;
// Do something with user here.
} else {
var message = result.message;
// Do something with message here.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.