You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在index.php中加载students.json数据并解决不显示问题

问题排查与解决方案

咱们一步步拆解你的学生数据无法显示的问题,逐个解决:

1. 先检查JSON文件名的拼写一致性

你描述里提到了student.json(少了末尾的s),但代码里调用的是students.json,首先要确认实际文件名称是students.json,和代码里的路径完全一致,文件名大小写也要注意(Windows系统不区分,但Linux/Mac是区分大小写的)。

2. 修复JavaScript语法错误

你的代码里拼接tblRow的字符串最后缺少分号,这会导致JS执行中断,浏览器控制台会直接报错。修正后的代码片段:

var tblRow = "<tr>" + 
    "<td>" + f.SNo + "</td>" + 
    "<td>" + f.StudentName + "</td>" + 
    "<td>" + f.AdmissionNo + "</td>" + 
    "<td>" + f.Subject + "</td>" + 
    "<td>" + f.Class + "</td>" + 
    "</tr>"; // 这里必须加上分号

3. 升级老旧的jQuery版本

你用的jQuery 1.6.2是2011年的版本,和现代浏览器兼容性很差,建议换成最新的稳定版(比如3.x系列):

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

4. 解决本地文件访问的跨域限制

如果你是直接双击打开index.php(通过file://协议访问),现代浏览器的同源策略会阻止读取本地JSON文件,这属于跨域操作。你需要用本地服务器运行代码:

  • 用PHP内置服务器:在项目文件夹打开终端,执行php -S localhost:8000,然后在浏览器访问http://localhost:8000/index.php
  • 或者用XAMPP/WAMP等服务器软件,把文件放到网站根目录,通过http://localhost/你的文件夹/index.php访问

5. 加上错误处理方便调试

在$.getJSON后面添加fail回调,能帮你快速定位请求失败的原因:

$.getJSON('students.json', function(data) {
    // 原有的渲染逻辑
})
.fail(function(jqXHR, textStatus, errorThrown) {
    console.log("请求失败: " + textStatus + ", 错误信息: " + errorThrown);
});

打开浏览器开发者工具(F12)的Console标签,就能看到具体的错误提示。


修正后的完整index.php代码

<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(function() {
    $.getJSON('students.json', function(data) {
        $.each(data.student, function(i, f) {
            var tblRow = "<tr>" + 
                "<td>" + f.SNo + "</td>" + 
                "<td>" + f.StudentName + "</td>" + 
                "<td>" + f.AdmissionNo + "</td>" + 
                "<td>" + f.Subject + "</td>" + 
                "<td>" + f.Class + "</td>" + 
                "</tr>";
            $(tblRow).appendTo("#userdata tbody");
        });
    })
    .fail(function(jqXHR, textStatus, errorThrown) {
        console.log("请求失败: " + textStatus + ", 错误信息: " + errorThrown);
    });
});
</script>
</head>
<body>
<div class="page">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" height="90%">
        <tr valign="top" align="center">
            <td>
                <table class="data-table" id="userdata">
                    <thead>
                        <tr>
                            <th>SNo.</th>
                            <th>Student Name.</th>
                            <th width='120'>Admission No</th>
                            <th>Subject</th>
                            <th>Class</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </td>
        </tr>
    </table>
</div>
</body>
</html>

确认students.json格式正确

确保你的JSON文件没有语法错误(比如逗号、引号是否配对),正确内容如下:

{
 "student": [
 { "SNo": "1", "StudentName": "YASHAA BANERJEE", "AdmissionNo": "01F7001", "Subject": "Science", "Class": 01 },
 { "SNo": "2", "StudentName": "SAKINA KASSIM MATCHESWALA", "AdmissionNo": "02G7015", "Subject": "Maths", "Class": 02 },
 { "SNo": "3", "StudentName": "AADITHYA PRADEEP", "AdmissionNo": "02D7021", "Subject": "Maths", "Class": 02 }
 ]
}

按照上面的步骤操作,应该就能正常显示学生数据了。

内容的提问来源于stack exchange,提问作者omkara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:29:57