我想用ajax進行重置密碼模塊,但是從PHP文件返回值(console.log()
)始終是空白。在我的php文件中也沒有錯誤,我甚至不會回顯空字符串。請幫助,這是我的Ajax請求Ajax的成功返回空字符串
$.ajax({
type:'POST',
url:'verify-change-pass',
data: {_oldpass : op, _pass1 : p1, _pass2 : p2},
success:function(data) {
data = data.trim();
switch(data){
case "er_no_acc" :
$('#grp_oldpass').addClass('has-error');
$('#feedback_oldpass').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_oldpass').innerHTML = "*No account found. Please refresh the page to generate new session.";
//alert('Please enter your username');
break;
case "er_oldpass_empty" :
$('#grp_oldpass').addClass('has-error');
$('#feedback_oldpass').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_oldpass').innerHTML = "*Please enter old password.";
// alert('Please enter your password');
break;
case "er_oldpass_invalid" :
$('#grp_oldpass').addClass('has-error');
$('#feedback_oldpass').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_oldpass').innerHTML = "*Old password does not match.";
// alert('Please enter your password');
break;
case "er_pass1_empty" :
$('#grp_pass1').addClass('has-error');
$('#feedback_pass1').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass1').innerHTML = "*Please enter password.";
// alert('Please enter your password');
break;
case "er_pass2_empty" :
$('#grp_pass2').addClass('has-error');
$('#feedback_pass2').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass2').innerHTML = "*Please enter password.";
// alert('Please enter your password');
break;
case "er_pass1_tooshort" :
$('#grp_pass1').addClass('has-error');
$('#feedback_pass1').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass1').innerHTML = "*Password must be more than 5 characters.";
// alert('Password too short.');
break;
case "er_pass2_tooshort" :
$('#grp_pass2').addClass('has-error');
$('#feedback_pass2').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass2').innerHTML = "*Password must be more than 5 characters.";
// alert('Password too short.');
break;
case "er_pass1_toolong" :
$('#grp_pass1').addClass('has-error');
$('#feedback_pass1').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass1').innerHTML = "*Password must be less than 20 characters.";
// alert('Password too long.');
break;
case "er_pass2_toolong" :
$('#grp_pass2').addClass('has-error');
$('#feedback_pass2').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass2').innerHTML = "*Password must be less than 20 characters.";
// alert('Password too long.');
break;
case "er_pass_notsame" :
$('#grp_pass1,#grp_pass2').addClass('has-error');
$('#feedback_pass1,#feedback_pass2').addClass('glyphicon glyphicon-remove form-control-feedback');
document.getElementById('msg_pass1').innerHTML = "*Password does not match.";
document.getElementById('msg_pass2').innerHTML = "*Password does not match.";
// alert('Password does not match');
break;
case "y" :
ResetMyPassword(p1);
break;
default:
alert('Error occured : '+data);
}
/* Always put console.log for debugging */
console.log(data);
}
});
這是我verify-change-pass.php
session_start();
if(!isset($_SESSION['username'])){
echo "Error 404 : Page not found.";
exit();
}
include("connectdb.php");
$par_uname = $_SESSION['username'];
$par_oldpass = isset($_POST['_oldpass']) ? $_POST['_oldpass'] : '';
$par_pass1 = isset($_POST['_pass1']) ? $_POST['_pass1'] : '';
$par_pass2 = isset($_POST['_pass2']) ? $_POST['_pass2'] : '';
trim($par_oldpass);
trim($par_pass1);
trim($par_pass2);
htmlspecialchars($par_oldpass);
htmlspecialchars($par_pass1);
htmlspecialchars($par_pass2);
$count=0;
$stmt = $mysqli->prepare("SELECT acc_hashedpass FROM admin_acc WHERE acc_uname=?");
$stmt->bind_param('s',$par_uname);
$stmt->execute();
$stmt->store_result();
$count = $stmt->num_rows;
$stmt->bind_result($hashed);
$stmt->fetch();
$stmt->close();
if($count != 1){
echo "er_no_acc"; //no username found. check the session if fetched properly
exit();
}
if ($par_oldpass == ""){
echo "er_oldpass_empty"; //oldpass is empty
exit();
}
if(!password_verify($par_oldpass,$hashed)){
echo "er_oldpass_invalid" . password_verify($par_oldpass,$hashed); //invalid old password
exit();
}
if ($par_pass1 == ""){
echo "er_pass1_empty"; //password 1 is empty
exit();
}
if($par_pass2 == ""){
echo "er_pass2_empty"; //password 2 is empty
exit();
}
if(mb_strlen($par_pass1) < 6){
echo "er_pass1_tooshort"; //password is too short. Must be more than 8 characters
exit();
}
if(mb_strlen($par_pass2) < 6){
echo "er_pass2_tooshort"; //password is too short. Must be more than 8 characters
exit();
}
if(mb_strlen($par_pass1) > 20){
echo "er_pass1_toolong"; //password is too long. Must be less than 20 characters
exit();
}
if(mb_strlen($par_pass2) > 20){
echo "er_pass2_toolong"; //password is too long. Must be less than 20 characters
exit();
}
if ($par_pass1 != $par_pass2){
echo "er_pass_notsame"; //password doesnt match
exit();
}
echo "y";
從verify-change-pass.php
的第一個高達6日if
回聲工作,之後,返回值始終是一個空字符串。請幫助..
順便說一句,這個片段適用於localhost
,但我在網上上傳後,該代碼不起作用。
的AJAX文件有一個在php文件沒有錯誤。我也嘗試在'include(connectdb.php)'之前回應,並且ajax返回成功。另外,如果我輸入了錯誤的舊密碼,則ajax請求會返回正確的值(即「er_oldpass_invalid」')。 –
我只是不明白爲什麼它可以在'localhost'中正常工作,但不能在線上傳。 :/ –