当我尝试检索 JSON 中的值时,收到未定义的错误。我是 ajax/js 等新手,并尝试创建一个“优雅”的下拉登录。
我尝试了各种方法并阅读了一些我在这里找到的帖子,但我注意到布局发生了一些变化,而且我还注意到我正在使用成功,但现在已弃用。
那么我可以请求帮助,首先了解问题是什么以及如何解决未定义的问题,其次实现此目标的最佳方法是什么。如果可以的话,我宁愿不使用已弃用的代码。
我还注意到,自从更改代码以便它到达 ajax 调用的“成功”公园后,下拉框不再回滚或显示错误消息。 -.-
提前致谢。
Ajax
function validLogin(){
$('.error').hide();
var username = $('#username').val();
if(username == ""){
$('label#usernameError').show();
$('input#username').focus();
return false;
}
$('.error').hide();
var password = $('#password').val();
if(password == ""){
$('label#passwordError').show();
$('input#password').focus();
return false;
}
var params = {username: username, password: password};
var url = "../js/loginProcessAjax.php";
$("#statusLogin").show();
$("#statusLogin").fadeIn(400).html('<img src="images/loading.gif" />');
$.ajax({
type: 'POST',
url: url,
data: params,
datatype: 'json',
beforeSend: function() {
document.getElementById("statusLogin").innerHTML= 'checking...' ;
},
success: function(data) {
alert("success Area ofAjax");
$("#statusLogin").hide();
if(data.success == true){
alert("if data.success Area of Ajax");
alert(data.message);
}else{
alert("data.message... " + data.message);//undefined
$("#errorConsole").html(data.message);
}
},
error: function( error ) {
console.log(error);
}
}, 'json');
}
PHP
<?php
if($_POST){
if($users->userExists($username) === false){
$data['message'] = "User doesn't exist";
$data['success'] = false;
}else if($users->userExists($username) === false){
$data['message'] = 'That username does not exist';
$data['success'] = false;
}else if($users->emailActivated($username) === false){
$data['message'] = 'You need to activate the account, please check your email.';
$data['success'] = false;
}else{
$login = $users->login($username, $password);
if($login === false){
$data['message'] = 'Incorrect Password or username';
$data['success'] = false;
}else{
$data['success'] = true;
//destroy old session and create new - prevents session fixation attacks
session_regenerate_id(true);
//all details are correct - the method returns the id to be sotred as a session
$_SESSION['id'] = $login;
}
echo json_decode($data);
}
}
标记:
<form method="post" action="" id="ourLoginFormID_JS">
<div class="ourContactFormElement2">
<label for="username">Username:</label>
<input type="text" id="username" name="username" autocomplete="off" class="required" value="<?php if(isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
</div>
<div class="ourContactFormElement2">
<label for="password">Password:</label>
<input type="password" id="password" name="password" autocomplete="off" class="required"/>
</div>
<div class="ourContactFormElement2">
<label> </label>
<input type="submit" name="loginButton" id="loginButton" value="Login!" onclick="validLogin(); return false;"/>
</div>
<div id="statusLogin"></div>
</form>
最佳答案
如果最后的 else
block 执行,您的 if/else/else/else 链仅输出 json。您需要将 json_encode 调用移到 block 之外:
if (...) {
} else if (...) {
} else if (...) {
} else {
...
}
echo json_encode($data);
这样,无论实际执行的是哪个 if() 子句,您的代码都将输出编码的 $data。
关于php - Ajax 从 JSON 返回未定义 - 登录系统和最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17749608/