0

データベースレコードを更新するために ajax リクエストを送信しています。html フォームを使用してテストし、正常に動作していますが、ajax リクエストを送信しようとすると、受信した応答は常に null です。htmlフォームの場合、正しい応答が表示されます。Windows OSで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);
            }
        });

complete関数はsuccess渡された異なるデータを取得します。successデータのみを取得し、全体を完了しますXMLHttpRequest

于 2013-03-25T20:26:10.550 に答える
0

まず、ajax リクエストで、jQuery が json を受信して​​いることを確実に理解できるように設定dataTypeする必要があります。json

次に、completeajax リクエストからのデータは渡されず、渡されるだけsuccessです。

これは私がまとめた完全な動作例であり、動作することがわかっています:

test.php (Web ブラウザーでこのページを呼び出します)

<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 に答える