我的提醒确实包含返回的数据和状态。
即使我抛出异常是另一个问题,我的状态总是“成功”。
这是我的div:
<div id="login_error">*</div>
我的jQuery代码是:
$('#submit_login').click(function(){
$userLogin = document.getElementById("user_login_name").value;
$userPassword = document.getElementById("user_password").value;
var returnedData = "";
$.post("login_ldap.php", {
userLogin: $userLogin,
userPassword: $userPassword
},
function(data, status){
alert("Data: " + data + '\nStatus: ' + status);
returnedData = data;
$('#login_error').append(returnedData);
//document.getElementById("login_error").value = data;
});
});
更新: 我添加了回调函数.done .fail&amp; 。总是 我的警报语句有效,但我显示的ID为#login_error的div不起作用
$.post("login_ldap.php", {userLogin: $userLogin, userPassword: $userPassword})
.done(function(data, status){
alert("Data: " + data + '\nStatus: ' + status);
$('#login_error').empty().append("done");
})
.fail(function(xhr, textStatus, errorThrown) {
alert("responseText " + xhr.responseText + "test Status " + textStatus + "errorThrown " + errorThrown);
// console.log("The following error occured: "+ textStatus, errorThrown);
$('#login_error').empty().append(xhr.responseText);
//$('#login_error').html(xhr.responseText);
})
.always(function() {
alert("finished");
});
我试过附加一个字符串而不是数据,而我的div中没有显示任何内容。
我的PHP代码是:
<?php
include_once 'classDB.php';
try {
$userLogin = "";
if (isset($_POST['userLogin'])) {
$userLogin = $_POST['userLogin'];
}
else {
echo "Enter Username";
return -1;
}
$userPassword = "";
if (isset($_POST['userPassword'])) {
$userPassword = $_POST['userPassword'];
}
else {
echo "Enter Password";
return -1;
}
echo classDB::login_ldap($userLogin, $userPassword);
}
catch ( Exception $e ) {
echo "LOGIN ERROR: " . $e->getMessage();
error_log($e->getMessage());
exit(-1);
}
?>
这是我的功能:
public static function login_ldap($userLogin, $userPassword) {
... this is all working
return "LOGIN SUCCESSFUL";
else
throw new Exception("LOGIN FAILED: binding error\nPlease try again.");
}