JQuery没有从php中获取回声

时间:2013-07-31 19:38:36

标签: php javascript jquery ajax echo

我正在回复从PHP脚本到JQuery ajax调用的消息('ok')。

我正在回显正确的消息,当我记录它时它会在控制台中显示,但是相应的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>

2 个答案:

答案 0 :(得分:1)

更改ajax调用中的dataType: "json"

然后在你的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对象。

答案 1 :(得分: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();