带有JS的JSON到HTML表没有最终输出
JSON to HTML Table with JS No final output
我正试图用JS添加到一个html表中,该表从JSON文件中获取信息。我不明白为什么这不会在表上显示任何数据。
JSON示例
[{"User_Name":"John Doe","score":"10","team":"1"},
{"User_Name":"Jane Smith","score":"15","team":"2"},
{"User_Name":"Chuck Berry","score":"12","team":"2"}];
我的JS不起作用:(
<table class="table">
<thead>
<tr>
<th data-field="date">Date</th>
<th data-field="message">Message</th>
<th data-field="votes">Votes</th>
</tr>
<tr>
<script type="text/javascript">
window.onload = function(){
$(document).ready(function () {
$.getJSON("/crestdale/new/db.json", function(raw){
console.log(raw);
var json = raw;
var tr;
console.log(json);
for (var i = 0; i < json.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + json[i].date + "</td>");
tr.append("<td>" + json[i].message + "</td>");
tr.append("<td>" + json[i].votes + "</td>");
$('table').append(tr);
}
});
});
};
</script>
</tr>
</thead>
</table>
新一期
我有一个php文件可以写入json文件,当我尝试使用此脚本添加到json文件时,它会使json文件不显示。
PHP
<?php
date_default_timezone_set('America/new_york');
$message = $_POST['message'];
$date = date("F j, Y, g:i a");
$fp = fopen('db.json', 'r+');
// Loading existing data:
$json = file_get_contents("db.json");
$data = json_decode($json, true);
// Adding new data:
$data[3] = array('date' => $date, 'message' => $message, 'votes' => $votes);
// Writing modified data:
file_put_contents('db.json', json_encode($data, JSON_FORCE_OBJECT));
fclose($fp);
?>
PHP之后的JSON
{"0":{"User_Name":"John Doe","score":"10","team":"1"},"1":{"User_Name":"Jane Smith","score":"15","team":"2"},"2":{"User_Name":"Chuck Berry","score":"12","team":"2"},"3":{"date":"February 16, 2015, 11:06 pm","message":null,"votes":null}}
db.json(请注意末尾没有分号)在您的文章中,您在json文件的末尾指定了一个分号。
[{"User_Name":"John Doe","score":"10","team":"1"},{"User_Name":"Jane Smith","score":"15","team":"2"},{"User_Name":"Chuck Berry","score":"12","team":"2"}]
如果您直接在javascript上使用它,那么它与分号配合使用会很好。
var json = [{"User_Name":"John Doe","score":"10","team":"1"},{"User_Name":"Jane Smith","score":"15","team":"2"},{"User_Name":"Chuck Berry","score":"12","team":"2"}];
在json文件中,分号是非法的。但在javascript中,分号是代码行的有效末尾。当我们使用$.getJSON()时,它会获取json文件的内容并对其进行解析。由于分号是非法字符;解析将失败。
您的javascript很好。但问题是在Json上找不到日期、信息和选票。我已经检查过了,这是有效的
$(document).ready(function () {
$.getJSON("db.json", function(raw){
var json = raw;
var tr;
console.log(json);
for (var i = 0; i < json.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + json[i].User_Name + "</td>");
tr.append("<td>" + json[i].score + "</td>");
tr.append("<td>" + json[i].team + "</td>");
$('table').append(tr);
}
});
});
新问题的解决方案
你确定$message和$votes被正确初始化了吗。
<?php
$date = date("F j, Y, g:i a");
$message='new message';
$votes=10;
$contents = file_get_contents('db.json');
$json = json_decode($contents,true);
$ar= array('date' => $date, 'message' => $message, 'votes' => $votes);
array_push($json,$ar);
$newJson = json_encode($json);
file_put_contents("db.json",$newJson);
?>
相关文章:
- 使用Jade输出HTML(在ng个重复内)
- 输出 html 表单数据
- 如何通过 AngularJS 模板输出 html
- Ajax 输出 html 注释
- 显示查询输出 HTML 中的图像
- HTML 代码不适用于 jquery 调用,而是输出 HTML 代码
- Html.ActionLink onclick JavaScript 函数未被调用,并且函数名称在输出 HTML 中已损
- Javascript -使用字符串输出HTML
- 如何正确使用jQuery从php输出html字符串
- 为什么.html()不输出html标签
- 如何获得一个变量's输出html值
- Twitter-text.js输出html作为纯文本
- 多行文本文件输出html
- 如何使用XSLT匹配元素和输出HTML
- 输出HTML代码
- 输出HTML时出现意外的令牌错误
- 用JavaScript输出HTML实体
- 在rjerb模板中输出HTML
- 从angularjs过滤器输出HTML,无需ng绑定HTML
- 获取实际输出HTML的函数