我无法找到我的代码有什么问题。从post_receiver.php打印json文件时,会相应地打印json。
从post_receiver.php打印的JSON
<?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
},...
&#39;计数&#39;和紧急情况&#39;变量没有定义,我不熟悉JSON格式...
答案 0 :(得分:1)
在success
回调中,您会收到一个data
字符串,其中包含回复。要将其解析为JSON,请使用json
dataType
设置:
$.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);
}
});