请注意大师对以下谜团的帮助。
我在我的 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);
?>
最佳答案
您需要添加一个 header 以在 PHP 脚本中将其输出为 json
:json_encoded_array.php
header("Content-type: application/json");
默认情况下,PHP 将返回 text/html
,这不是 $.getJSON()
JSON 文本的 MIME 媒体类型是 application/json。默认编码为 UTF-8。 (来源:RFC 4627)。
关于javascript - PHP 脚本 json_encode mysql 请求无法传递给 getJSON(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27902181/