gpt4 book ai didi

Javascript 无法处理 JSON 文本文件

转载 作者:行者123 更新时间:2023-11-30 22:52:05 27 4
gpt4 key购买 nike

我正在按照一些教程从服务器传递 JSON 文本文件,以在对 html 文件进行一些 javascript 处理后显示数据。作为测试,尝试显示一列的 LI,但无法在浏览器中获得任何输出。感谢您的帮助。

我尝试了两种方法。

方法 1 xmlhttp:

显然,浏览器提示 html 格式:未捕获的语法错误:意外的字符串(15:08:42:080 | 错误,javascript) 在 testJSON3.html:12

我的 xmlhttp 调用格式是否正确?

提前感谢您的帮助。

这是 JSON 文本 myTutorial.txt:

[
{
"active":"1",
"courseCode":"208.01.00",
"courseName":"course name 1",
"eventDesc":"2015 class of course name 1"
},
{
"active":"1",
"courseCode":"208.01.00",
"courseName":"course name21",
"eventDesc":"2015 class of course name "
}
]

并通过下面的html处理来处理xmlhttp访问服务器localhost目录phpTWLLT上的文件

<!DOCTYPE html>


<html>
<head>
<script type='text/javascript' src='js/jquery.min.js'></script>
<meta charset="UTF-8">
</head>
<body>

<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost/phpTWLLT/myTutorial.txt";

xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(arr) {
var out = "";
var i;
for (i = 0; i < arr.length; i++) {
out += '<li'> + arr[i].courseCode +'</li><br>';
}
document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>

方法 2 getJSON():

这个很有趣。如果服务器端是静态数组($DEBUG = true:),javascript 能够处理并得到浏览器显示。但是从 mysql ($DEBUG = false) 生成文本时失败。

我正在绞尽脑汁让 $DEBUG=false 工作?显然,这两种情况都生成了有效的 JSON 文本。

如果 $DEBUG 设置为真,

从 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"}]

浏览器中显示的列表。01

如果 $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);
?>

最佳答案

对于方法 2,您是否尝试调试 javascript 代码以检查数据变量是否包含预期数据?

您还可以检查网络选项卡以查看从您的服务器发送的响应数据是否正确。

关于Javascript 无法处理 JSON 文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27896953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com