ajax返回错误但该过程成功

时间:2017-09-29 07:39:39

标签: ajax

每次运行时,我的ajax总会返回错误但是当我刷新页面时,每次触发时都会更新数据

我的ajax

    $.ajax({
    url:"exe/deduct.php",`enter code here`
    method:"POST",
    data: {deductQty: deductQty, itemID: itemID, oldQty: oldQty},
    success: function(data){
         alert(data);
         window.location.href="deduct.php";
    },
    error: function(jqXHR, exception){
         alert(data);
    }
});

我的PHP代码

    <?php
    include('database.php');
    if($_POST){
      $itemID = $_POST['itemID'];
      $deductQty = $_POST['deductQty'];
      $oldQty = $_POST['oldQty'];

      $answer = $oldQty - $deductQty;

      $query = "UPDATE `item` SET `qty` = '$answer' WHERE `id` = '$itemID'";
      if(mysqli_query($con,$query)){
          echo "Restock Success";
      }else{
          echo "Restock Failed";
      }
    }

?>

1 个答案:

答案 0 :(得分:0)

确保url位置准确无误。

response可以在div $('#divid').html(response);

中打印
$.ajax({
url:"exe/deduct.php",
method:"POST",
data: {deductQty: deductQty, itemID: itemID, oldQty: oldQty},
success: function(response){
     alert(response);
     //$('#divid').html(response);
     //window.location.href="deduct.php";
},
error: function(jqXHR, exception){
    var message = '';

    if (jqXHR.status === 0)
    message = 'Not connect.\n Verify Network.';
    else if (jqXHR.status == 404)
    message = 'Requested page not found. [404]';
    else if (jqXHR.status == 500)
    message = 'Internal Server Error [500].';
    else if (exception === 'parsererror')
    message = 'Requested JSON parse failed.';
    else if (exception === 'timeout')
    message = 'Time out error.';
    else if (exception === 'abort')
    message = 'Ajax request aborted.';
    else
    message = 'Uncaught Error.\n' + jqXHR.responseText;

    alert(message);
}
});
相关问题