0

我正在将一条消息('ok')从 PHP 脚本回显到 JQuery ajax 调用。

我正在回显正确的消息,并且当我记录它时它会显示在控制台中,但是没有触发相应的 jquery 函数 - 根据代码我应该得到“您的密码已成功更改”消息,但我只收到“出现问题”消息 - 任何人都可以提出原因吗?

这是PHP的代码:

if(isset($_POST['oldpass'])){
    $oldpass = mysql_real_escape_string($_POST['oldpass']);
    $newpass = mysql_real_escape_string($_POST['newpass']);
    $sql = "SELECT password, salt FROM users WHERE email='$log_email' AND id='$log_id' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $numrows = mysqli_num_rows($query);
    if($numrows > 0){
        while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
            $current_salt = $row["salt"];
            $db_pass = $row["password"];
        }

        $old_pass_hash = crypt($oldpass, $current_salt);
        if ($old_pass_hash != $db_pass){
            echo "problem";
            exit();
        }
    }
    $s = "$2a$10$";
    $random = randStrGen(20);
    $salt = $s.$random;
    $p_crypt = crypt($newpass, $salt);
    $sql = "UPDATE users SET password='$p_crypt', salt='$salt' WHERE email='$log_email' AND id='$log_id' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    if ($query == true){
        $_SESSION['password'] = $p_crypt;
        echo 'ok';
        exit();
    } 
}
?>

这是 javascript/JQuery

function change_password(){
    var oldpass = $('#old_pass').val();
    var newpass = $('#new_pass').val();
    var newpass_conf = $('#confirm_new_pass').val();

    if(newpass != newpass_conf){
        $('#status').html("Your passwords do not match");
    } else if(newpass=="" || oldpass==""){
        $('#status').html("You have not entered anything");
    }
    $.ajax({
      type: 'POST',
      url: "changePassword.php",
      dataType: 'text',
      data: {
          "oldpass": oldpass,
           "newpass": newpass_conf    },
      success:function(data){

         if(data == "ok"){
              $('#change_password_form').html("<h2> Success</h2><div class='noerror'><p> Your password has been changed successfully.</p> <p> You may now close this window.</p></div>");
          } else {
              $('#status').html("There was a problem");
          }
       }
    });
}

$(document).ready(function(){
    $(document).on('click','#change_password', function(){
    change_password();
});


});
</script>

最后是html

<div> <h1>Change your password</h1></div><hr>
  <form id="change_password_form" class="input" onsubmit="return false;">
  <div> <label for="old_pass">Current Password:</label>
    <input id="old_pass" type="text" class="searchbox" onfocus="emptyElement('status')" maxlength="88" value=""></div>
  <div> <label for="new_pass">New Password:</label>
    <input id="new_pass" class="searchbox" type="text"  onfocus="emptyElement('status')" maxlength="88" value=""> </div>
 <div><label for="confirm_new_pass">Confirm New Password:</label>
    <input id="confirm_new_pass" class="searchbox" type="text"  onfocus="emptyElement('status')" maxlength="88" value=""><div> 
  <input type="button" style="position:relative;top:10px; float:right;" id="change_password" value="Change Password"></form>
    <span id="status" class="statuserror"></span>
</body>
</html>
4

2 回答 2

1

改变dataType: "json"你的ajax调用

然后在你的 php 代码中返回 json 数据
json_encode(array('response'=>'ok'));

你的 ajax 成功函数应该是这样的,

success: function (data) {
    var resultObject = jQuery.parseJSON(data);
        if(rersultObject['response']=='ok') {
            $('#change_password_form').html("<h2> Success</h2><div class='noerror'><p> Your password has been changed successfully.</p> <p> You may now close this window.</p></div>");
        } else {
            $('#status').html("There was a problem");
        }
    }
}`

这里parseJSON用于将 JSON 字符串转换为 javascript 对象。

于 2013-08-01T07:09:46.280 回答
0

我前一阵子遇到了这个问题,尽管情况不同,但我永远无法弄清楚。我所做的是将数据类型更改为json

$.ajax({
    type: 'POST',
url: url,
data: 'data=data&other=other'
dataType: 'json',
//if everything goes out as planned
success: function(response) { 
       alert(response['data']);
}
}); 

并在 php

 $respond = array("data" => 'ok',
   "other" => 'whatever else'
  );
echo json_encode($respond);  //send a response back to javascript
exit();
于 2013-07-31T19:53:51.663 回答