2017-07-15 86 views
0

我無法找到我的代碼有什麼問題。當從post_receiver.php打印json文件時,json會相應地打印。ajax,變量未定義?

的JSON從post_receiver.php印刷從

 <?php 
     session_start(); 
     ob_start();      
     require_once('../../mysqlConnector/mysql_connect.php'); 
     $result_array = array(); 

    $query="SELECT COUNT(initID) AS count, urgency, crime, initID, TIMESTAMPDIFF(minute,dateanalyzed,NOW()) AS minuteDiff FROM initialanalysis WHERE commanderR='0' AND stationID='{$_SESSION['stationID']}';"; 

$result=mysqli_query($dbc,$query); 
    if ($result->num_rows > 0) { 

    while($row = $result->fetch_assoc()) { 

     array_push($result_array, $row); 

     } 

          } 
    echo json_encode($result_array); 
          ?> 

結果以上:

[{"count":"10","urgency":"Low","crime":"Firearm","initID":"5","minuteDiff":"329"}] 

我的Ajax代碼:

$.ajax({ 
     method: 'POST', 
     url: "post_receiver.php", 
     data: { 
      'count': count, 
      'urgency': urgency 
     },... 

的 '計數' 和 '尿急' 可變沒有定義,我不熟悉JSON格式...

+1

的'data'字段指定什麼請求參數* *發送到服務器。你想發送一個「count」和「urquency」值,還是你想從響應中提取它們? – PeterMader

+0

@PeterMader我想從響應 – SCS

回答

1

在你的success回調中,你會得到一個data字符串,其中包含響應。解析它作爲JSON,使用jsondataType設置:

$.ajax({ 
    method: 'POST', 
    url: 'post_receiver.php', 
    dataType: 'json', 
    success: function (data) { 
    // 'data' contains the parsed JSON 
    console.log('Count:', data[0].count); // read the values from the JS object and log them to the console 
    console.log('Urgency:', data[0].urgency); 
    } 
}); 
+0

中提取值,這是行不通的。而不是成功的阿賈克斯,它去了錯誤捕手。 – SCS

+1

什麼是錯誤? – PeterMader

+0

添加了我的php代碼,它只是在控制檯中輸出結果。 「error:function(XMLHttpRequest,textStatus,exception){console.log(XMLHttpRequest.responseText);」 – SCS