Javascript PHP脚本json_encode mysql请求无法传递到getJSON()
请注意古鲁对以下奥秘的帮助 我在html中使用了Javascript PHP脚本json_encode mysql请求无法传递到getJSON(),javascript,mysql,arrays,getjson,Javascript,Mysql,Arrays,Getjson,请注意古鲁对以下奥秘的帮助 我在html中使用了getJSON() 只有硬编码的数组才能进行json_编码(即通过设置$DEBUG=true:)并传递到javascript,然后浏览器显示结果。但是从mysql生成文本时失败(通过设置$DEBUG=false) 为了让mysql生成的动态数组正常工作,我绞尽脑汁?我可以在浏览器中运行这两种场景,并在浏览器中输出JSON格式的文本,即 如果设置了$DEBUG,true localhost/phpTWLLT/json_encode_array.php
getJSON()
只有硬编码的数组才能进行json_编码(即通过设置$DEBUG=true
:)并传递到javascript,然后浏览器显示结果。但是从mysql生成文本时失败(通过设置$DEBUG=false
)
为了让mysql生成的动态数组正常工作,我绞尽脑汁?我可以在浏览器中运行这两种场景,并在浏览器中输出JSON格式的文本,即
如果设置了$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"}]
[{"active":"1"},{"active":"1"}]
在浏览器中显示的列表。
0
一,
如果设置了$DEBUG
,false
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"}]
[{"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>
$(文档).ready(函数(){
/*调用包含json_编码的php数组的php*/
//$.getJSON('json_encoded_array.php',函数(数据){
$.getJSON('json\u encoded\u array.php',函数(数据){
/*数据将把php数组作为javascript对象保存*/
$。每个(数据、函数(键、值){
$('ul').append(''+val.active+' ');
});
});
});
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("Content-type: application/json");
您需要添加一个标头,以便在PHP脚本中将其输出为json
: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("Content-type: application/json");
默认情况下,PHP将返回text/html
,对于$.getJSON()
JSON文本的MIME媒体类型为application/JSON。默认编码为UTF-8。(源代码:)。您需要添加一个标头,以在PHP脚本中作为JSON
输出:JSON\u encoded\u 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("Content-type: application/json");
默认情况下,PHP将返回text/html
,对于$.getJSON()
JSON文本的MIME媒体类型为application/JSON。默认编码为UTF-8。(来源:)。$。每个
都需要一个javascript数组或对象来循环而不是一个JSON字符串
首先需要使用
jQuery.parseJSON()
因此,您的代码将如下所示
data = jQuery.parseJSON(data);
$.each(data, function (key, val) {
$('ul').append('<li id="' + key + '">' + val.active + '</li>');
});
data=jQuery.parseJSON(数据);
$。每个(数据、函数(键、值){
$('ul').append(''+val.active+' ');
});
$。每个
都需要一个javascript数组或对象来循环执行而不是json字符串
,首先需要使用
jQuery.parseJSON()
因此,您的代码将如下所示
data = jQuery.parseJSON(data);
$.each(data, function (key, val) {
$('ul').append('<li id="' + key + '">' + val.active + '</li>');
});
data=jQuery.parseJSON(数据);
$。每个(数据、函数(键、值){
$('ul').append(''+val.active+' ');
});
发现来自Netbeans的默认许可证头(自动生成)阻止Javascript识别JSON结构
删除默认许可证标题后,将默认许可证标题修改为空。PHP脚本的输出仅包含JSON结构。浏览器显示正确
请比较下面的两个调试输出(无法发布图像)
debugFalse4.jpg:
debugfalsewking.jpg:
非常感谢Saquieb展示了如何获取调试信息!!!发现来自Netbeans(自动生成)的默认许可证头阻止Javascript识别JSON结构
删除默认许可证标题后,将默认许可证标题修改为空。PHP脚本的输出仅包含JSON结构。浏览器显示正确
请比较下面的两个调试输出(无法发布图像)
debugFalse4.jpg:
debugfalsewking.jpg:
非常感谢Saquieb展示了如何获取调试信息!!!他甚至可以使用$。ajax
设置数据类型:“JSON'
。甚至不必介意在PHP上设置头,只需返回一个JSON字符串。可以这样做,但使用头返回有效的头JSON是一个很好的实践(“内容类型:application/json”);
@Saqueib添加了标题,但没有更改浏览器output@makarthur:您需要将标题设置在if
条件之外,以便在这两种情况下都起作用。您只是通过了“active”"通过您的secound条件,您希望它返回什么?与第一个相同?@makarthur如果您可以共享控制台屏幕截图,这将非常有帮助,他甚至可以使用$。ajax
设置数据类型:'JSON'
。甚至不必介意在PHP上设置标题,只需返回JSON字符串。是的,这可以做到,但它很好使用标题(“内容类型:application/json”);
@Saqueib添加了标题,但未更改浏览器,返回有效的标题json的操作output@makarthur:您需要将标题设置在if
条件之外,以便在这两种情况下都起作用。您只需传递“active”通过您的secound条件,您希望它返回什么?与第一个相同?@makarthur如果您可以共享控制台屏幕截图,它将非常有帮助尝试上面的代码,既不是$DEBUG=true也不是$DEBUG=false生成输出。尝试上面的代码,既不是$DEBUG=true也不是$DEBUG=false生成输出。输出错误未捕获SYNTEXERROR:UNERtestGetJson.html:1>上的预期标记o(22:06:01:876|error,javascript)尝试了上述代码,没有$DEBUG=true或$DEBUG=false生成输出。尝试了上述代码,没有$DEBUG=true或$DEBUG=false生成输出。输出错误未捕获语法错误:testGetJson.html:1>上的意外标记o(22:06:01:876|error,javascript)