0

我正在发送一个 ajax 请求来更新数据库记录,它使用 html 表单对其进行测试,它工作正常,但是当我尝试发送 ajax 请求时它的工作,但我收到的响应始终为空。在 html 表单上,它显示正确的响应。我在 Windows 操作系统上使用 xampp。请引导我正确的方向。

<?php
    header('Content-type: application/json');
    $prov= $_POST['prov'];
    $dsn = 'mysql:dbname=db;host=localhost';
    $myPDO = new PDO($dsn, 'admin', '1234');

    $selectSql = "SELECT abcd FROM xyz WHERE prov='".mysql_real_escape_string($prov)."'";
    $selectResult = $myPDO->query($selectSql);

    $row = $selectResult->fetch();
    $incr=intval($row['votecount'])+1;

    $updateSql = "UPDATE vote SET lmno='".$incr."' WHERE prov='".mysql_real_escape_string($prov)."'";
    $updateResult = $myPDO->query($updateSql);

    if($updateResult !== False) 
    {
    echo json_encode("Done!");
    } 
    else 
    {
    echo json_encode("Try Again!");
    }
    ?>




function increase(id)
    {
         $.ajax({
            type: 'POST',
            url: 'test.php',
            data: { prov: id },
            success: function (response) {

            },
            complete: function (response) {
                var obj = jQuery.parseJSON(response);
                alert(obj);
            }
        });
    };
4

2 回答 2

1
$.ajax({
            type: 'POST',
            url: 'test.php',
            data: { prov: id },
            dataType: 'json',
            success: function (response) {
                // you should recieve your responce data here
                var obj = jQuery.parseJSON(response);
                alert(obj);
            },
            complete: function (response) {
                //complete() is called always when the request is complete, no matter the outcome so you should avoid to recieve data in this function
                var obj = jQuery.parseJSON(response.responseText);
                alert(obj);
            }
        });

completesuccess函数获取不同的数据传入。success只获取数据,完成整体XMLHttpRequest

于 2013-03-25T20:26:10.550 回答
0

First off, in your ajax request, you'll want to set dataType to json to ensure jQuery understands it is receiving json.

Secondly, complete is not passed the data from the ajax request, only success is.

Here is a full working example I put together, which I know works:

test.php (call this page in your web browser)

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
  // Define the javascript function
  function increase(id) {
    var post_data = {
      'prov': id
    }

    $.ajax({
      'type': 'POST',
      'url': 'ajax.php',
      'data': post_data,
      'dataType': 'json',
      'success': function (response, status, jQueryXmlHttpRequest) {
        alert('success called for ID ' + id + ', here is the response:');
        alert(response);
      },
      'complete': function(jQueryXmlHttpRequest, status) {
        alert('complete called');
      }
    });
  }

  // Call the function
  increase(1); // Simulate an id which exists
  increase(2); // Simulate an id which doesn't exist
</script>

ajax.php

<?php
$id = $_REQUEST['prov'];

if($id == '1') { 
  $response = 'Done!';
} else {
  $response = 'Try again!';
}

print json_encode($response);
于 2013-03-25T20:23:35.503 回答