真正注意到下面神秘的大师的帮助。
我在html中使用了getJSON()
。
只有硬编码数组可以是json_encode(即通过设置$DEBUG = true
:)并传递给javascript,然后浏览器显示结果。但是从mysql生成文本时失败(通过设置$DEBUG = false
)。
我正在挠头让mysql生成的动态数组工作?我可以在浏览器中运行两个场景,在浏览器中使用JSON格式化文本输出,即http://www.example.com/phpTWLLT/json_encoded_array.php。
如果$DEBUG
设置为true
,
从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"}]
浏览器中显示的列表。 0 1
如果$DEBUG
设置为false
,
从localhost / phpTWLLT / json_encode_array.php输出
[{"active":"1"},{"active":"1"}]
浏览器显示为空白。
html文件:
<!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脚本: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);
?>
答案 0 :(得分:3)
您需要在PHP脚本中添加标题以将其输出为json
:json_encoded_array.php
header("Content-type: application/json");
默认情况下,PHP将返回text/html
,这不是$.getJSON()
的有效JSON
JSON文本的MIME媒体类型是application / json。默认编码为UTF-8。 (来源:RFC 4627)。
答案 1 :(得分:1)
$.each
使用javascript数组或对象循环遍历not a json string
首先需要使用
jQuery.parseJSON()
所以你的代码看起来像这样
data = jQuery.parseJSON(data);
$.each(data, function (key, val) {
$('ul').append('<li id="' + key + '">' + val.active + '</li>');
});
答案 2 :(得分:1)
发现Netbeans的默认许可标头(自动生成)会阻止Javascript识别JSON结构。
通过将默认许可证标题修改为空白来删除默认许可证标题后。 PHP脚本的输出仅包含JSON结构。浏览器显示正确。
请比较下面的两个调试输出(无法发布图像) debugFalse4.jpg:http://www.arthurmak.hk/debugFalse4.jpg debugFalseWrking.jpg:http://www.arthurmak.hk/debugFalseWorking.jpg
非常感谢Saquieb展示如何获取调试信息!!!