Reputation: 1212
I cant find whats is wrong with my code. When printing the json file from the post_receiver.php, the json is printed accordingly.
The JSON printed from the 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);
?>
Result from above:
[{"count":"10","urgency":"Low","crime":"Firearm","initID":"5","minuteDiff":"329"}]
my ajax code:
$.ajax({
method: 'POST',
url: "post_receiver.php",
data: {
'count': count,
'urgency': urgency
},...
the 'count' and 'urgency' variable is not defined, i am not that familiar with JSON format...
Upvotes: 0
Views: 355
Reputation: 7285
In your success
callback, you get a data
string, which contains the response. To parse it as JSON, use the json
dataType
setting:
$.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);
}
});
Upvotes: 1