Ajax使用json前后台交互
2018-04-02 19:37
309 查看
前后台分离,虚拟后台传回来的json格式数据文件数据goodslist.json文件如下:
<thead>
<tr>
<th>id</th>
<th>展品id</th>
<th>时间</th>
<th>展商姓名</th>
<th>详情</th>
</tr>
</thead>
<tbody id="goods"></tbody>
</table>注意一定要进入jQuery头文件:<script type="text/javascript" src="jquery.min.js"></script>ajax请求具体如下: $.ajax({
type:"GET",
url:"goodslist.json",
dataType:"json",
success:function(data){
list="";
//i表示在data中的索引位置,n表示包含的信息的对象
$.each(data,function(i,result){
//获取对象中属性
list+="<tr>"
list+="<td>"+result["id"]+"</td>";
list+="<td>"+result["exhibitorId"]+"</td>";
list+="<td>"+result["creatTime"]+"</td>";
list+="<td>"+result["exhibitsName"]+"</td>";
list+="<td>"+result["intro"]+"</td>";
list+="</tr>";
});
$('#goods').html(list);
},
error : function(error) {
alert("error");
}
});
[ {"id":"01","exhibitorId":"0001","creatTime":"2017-10-2","exhibitsName":"张三","intro":"简介"}, {"id":"02","exhibitorId":"0001","creatTime":"2017-10-2","exhibitsName":"张三","intro":"简介"}, {"id":"03","exhibitorId":"0001","creatTime":"2017-10-2","exhibitsName":"张三","intro":"简介"}, {"id":"04","exhibitorId":"0001","creatTime":"2017-10-2","exhibitsName":"张三","intro":"简介"} ]要将上述json数据通过ajax获取渲染到页面表格中: <table border="1">
<thead>
<tr>
<th>id</th>
<th>展品id</th>
<th>时间</th>
<th>展商姓名</th>
<th>详情</th>
</tr>
</thead>
<tbody id="goods"></tbody>
</table>注意一定要进入jQuery头文件:<script type="text/javascript" src="jquery.min.js"></script>ajax请求具体如下: $.ajax({
type:"GET",
url:"goodslist.json",
dataType:"json",
success:function(data){
list="";
//i表示在data中的索引位置,n表示包含的信息的对象
$.each(data,function(i,result){
//获取对象中属性
list+="<tr>"
list+="<td>"+result["id"]+"</td>";
list+="<td>"+result["exhibitorId"]+"</td>";
list+="<td>"+result["creatTime"]+"</td>";
list+="<td>"+result["exhibitsName"]+"</td>";
list+="<td>"+result["intro"]+"</td>";
list+="</tr>";
});
$('#goods').html(list);
},
error : function(error) {
alert("error");
}
});
相关文章推荐
- struts2使用ajax实现页面和action之间通过json数据交互
- jquery raty星级评分插件的具体使用(可以结合ajax和后台交互)以及点赞功能的实现
- 如何使用ajax将json传入后台数据
- 关于客户端与服务器ajax使用json数据交互,出现中文乱码或因包含html标答而报错的问题(base64加密)
- .NetMVC与EXT4使用json进行前后台交互
- 给ajax留一个模板,通过后台传递json获取数据,并用table显示,方便以后使用
- Java中使用json与前台Ajax数据交互的方法
- Jquery EasyUI +Ajax +Json +一般处理程序 实现数据的前台与后台的交互 --- 善良公社项目
- 使用JSON-RPC-Java完成Ajax交互
- 使用Ajax、json实现京东购物车结算界面的数据交互实例
- 前端使用Jquery的getJSON方法,后台使用PHP,解决IE和火狐Firefox中存在的跨域ajax请求问题。
- 使用AJAX与后台交互局部数据
- Ajax使用JSON将对象、集合传递给后台的方式(用于同时对多个对象进行修改)
- 使用ajax与springmvc后台交互
- 前端boostrap架构下使用ajax传值json数据给后台
- Jquery EasyUI +Ajax +Json +一般处理程序 实现数据的前台与后台的交互 --- 善良公社项目
- ASP.NET利用JQuery中的Aajax实现JSON数据后台交互
- Django中使用Ajax进行前后台交互
- html中使用Ajax与后台接口api交互(以登录功能为例)
- 前后台交互:跨域以及PHP与Ajax的配合使用