Really note help from guru on the below mystery.
I used getJSON() in my html.
Only hardcoded array can be json_encode (i.e. by setting $DEBUG = true:) and pass to javascript and subsequently browser display the result. But fail when generate the text from mysql (by setting $DEBUG = false).
I am scratching my head to get mysql generated dynamic array to work? I can run both scenario in the browser with JSON formated text output in the browser, i.e http://www.example.com/phpTWLLT/json_encoded_array.php.
If $DEBUG is set true,
output from localhost/phpTWLLT/json_encode_array.php
[{"active":"0","first_name":"Darian","last_name":"Brown","age":"28","email":"darianbr@example.com"},{"active":"1","first_name":"John","last_name":"Doe","age":"47","email":"john_doe@example.com"}]
the list displayed in browser. 0 1
If $DEBUG is set false,
output from localhost/phpTWLLT/json_encode_array.php
[{"active":"1"},{"active":"1"}]
The browser display is blank.
html file:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<!--
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'> </script>
-->
<script type='text/javascript' src='js/jquery.min.js'></script>
<meta charset="UTF-8">
</head>
<body>
<!-- this UL will be populated with the data from the php array -->
<ul></ul>
<script type='text/javascript'>
$(document).ready(function () {
/* call the php that has the php array which is json_encoded */
//$.getJSON('json_encoded_array.php', function(data) {
$.getJSON('json_encoded_array.php', function (data) {
/* data will hold the php array as a javascript object */
$.each(data, function (key, val) {
$('ul').append('<li id="' + key + '">' + val.active + '</li>');
});
});
});
</script>
</body>
</html>
PHP script: json_encoded_array.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/* set out document type to text/javascript instead of text/html */
$DEBUG = true;
if ($DEBUG) {
header("Content-type: text/javascript");
$arr = array(
array(
"active" => "0",
"first_name" => "Darian",
"last_name" => "Brown",
"age" => "28",
"email" => "darianbr@example.com"
),
array(
"active" => "1",
"first_name" => "John",
"last_name" => "Doe",
"age" => "47",
"email" => "john_doe@example.com"
)
);
} else {
require_once('connection.php');
// $m_id= 8 has many enrolled course and 11 got exactly one course enrolled.
$m_id = 8;
$p_id = 1;
$qry1 = "SELECT distinct event.active as active, subject.code as 'courseCode', subject.name as 'courseName', event.event_desc as 'eventDesc' FROM applicant, event, subject, part where applicant.applicant_id = $m_id and applicant.event_id = event.id and event.subject_id=subject.id and part.id = subject.owner_id and part.id = $p_id order by event.active DESC, event.from_month DESC ";
mysqli_set_charset($bd, 'utf-8');
$result = mysqli_query($bd, $qry1);
$arr = array();
$i = 0;
if (mysqli_num_rows($result) > 0) {
while ( $rs = mysqli_fetch_assoc($result) ) {
$colhead = "active";
$str = $rs['active'];
$arr[$i] = array($colhead => $str);
$i++;
// just generate two record for testing
if ($i === 2)
break;
}
}
}
echo json_encode($arr);
?>
You need to add a header to output it as json in PHP script: json_encoded_array.php
header("Content-type: application/json");
By default PHP will return text/html which is not a valid JSON for $.getJSON()
The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627).
$.each takes a javascript array or object to loop through not a json string first you need to parse your json string using
jQuery.parseJSON()
so your code will look like this
data = jQuery.parseJSON(data);
$.each(data, function (key, val) {
$('ul').append('<li id="' + key + '">' + val.active + '</li>');
});
Found that default License Header from Netbeans (which is automatically generated) prevented Javascript from recognizing the JSON structure.
After removing the default License Header by modifying the default License Header to blank. The output from PHP script only contains the JSON structure. The browser display correctly.
Please compare the two debug output below (Cannot post images) debugFalse4.jpg: http://www.arthurmak.hk/debugFalse4.jpg debugFalseWrking.jpg: http://www.arthurmak.hk/debugFalseWorking.jpg
Thanks a lot to Saquieb showing how to obtain the debug information!!!
来源:https://stackoverflow.com/questions/27902181/php-script-json-encode-mysql-request-cannot-pass-through-to-getjson