前端怎么显示后台获取的json格式数据
后端getdata.php代码如下:
<?php include('./conn/conn.php'); $query= "select id,content,pubtime,imgsrc,thumbsrc from messages order by id desc"; $result=mysql_query($query); while( $row = mysql_fetch_array( $result ) ) { $json[] = $row; } echo json_encode($json); exit; ?>
前端我想这么显示:
<div class='message' id='message'>第 $id 位用户: <span>$content</span><p><a href='./$imgsrc' title='点击查看原图'><img src='./$thumbsrc'></a></p><br>$pubtime</div>
前端javascript代码:
<script type="text/javascript"> function get_data() { $.ajax({ url: 'getdata.php', datatype:'json', success: function(data) { var data = eval("(" + data + ")"); ... //然后这个地方应该怎么写 } } } }); } setInterval("get_data()",3000); </script>
纯洁友善初次君
11 years, 11 months ago
Answers
<script type="text/template" id="template"> <div class='message' id='message'>第 {{id}} 位用户: <span>{{content}}</span><p><a href='./{{imgsrc}}' title='点击查看原图'><img src='./{{thumbsrc}}'></a></p><br>{{pubtime}}</div> </script> <script> (function($) { var template = $('#template').html(); function get_data() { $.ajax({ url: 'getdata.php', dataType: 'json', success: function(data) { for(var i = 0; i < data.length; ++i) { var tmp = data[i]; // 循环输出,用变量替换template中的{{变量}} } } }); } get_data(); })(jQuery); </script>
不吸血的蚊子
answered 11 years, 11 months ago