2013-03-25 24 views
0

我發送一個ajax請求來更新數據庫記錄,它使用html格式測試它,它的工作正常,但是當我嘗試發送ajax請求它的工作,但我收到的響應始終爲空。在html上表現出正確的迴應。我在Windows操作系統上使用xampp。請引導我正確的方向。如何處理來自php的json響應?

<?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); 
      } 
     }); 
    }; 
+0

你只是返回一個字符串,你不需要'json_encode'它,只是讓你的'的dataType :「文字」 – 2013-03-25 20:22:40

回答

0

首先,在你的Ajax請求,你要設置dataTypejson確保jQuery的理解它是接收JSON。

其次,complete沒有通過ajax請求的數據,只有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); 
+0

仍然無法使用! – Desire 2013-03-25 20:27:11

+0

你錯誤控制檯說什麼?什麼? – 2013-03-25 20:27:35

+0

沒有錯誤!我想知道爲什麼它在html表單上工作正常!並沒有與阿賈克斯請求工作:( – Desire 2013-03-25 20:29:19

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