2013-03-25 7 views
0

데이타베이스 레코드를 업데이트하라는 요청을 보내고 있습니다. html 양식을 사용하여 작동하는지 검사합니다. 그러나 Ajax 요청을 보내려고하면 작동하지만 응답은 항상 null입니다. html 형식의 경우 정확한 응답을 보여줍니다. Windows OS에서 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' 할 필요가 없습니다 완료 : "text"' –

답변

0

우선 해제, 당신의 아약스 요청에서, 당신은 dataTypejson에이 코드에서는 jQuery JSON을 받고 이해하기 위해 설정할 수 있습니다.

두 번째로, complete은 아약스 요청의 데이터를 전달하지 않으며 success 만 있습니다.

test.php (웹 브라우저에서이 페이지를 호출)

<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

+0

콘솔에 어떤 오류가 있습니까? 아무것도? –

+0

오류가 없습니다! 왜 HTML 폼에서 잘 작동하는지 궁금합니다! 아약스 요청으로 작동하지 않는 :( – Desire

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

관련 문제