layui 利用ajax冲获取到json 数据后 怎样进行渲染

如题所述

1、新建一个html文件,命名为test.html。

2、在test.html中,使用script标签加载jquery.min.js文件,这是使用jquery方法的前提。

3、在test.html页面中,创建一个button按钮,用于实现点击通过ajax请求获得json数据,在button下面再创建一个p元素,用于json数据的显示。

4、新建一个文件名为ajax_json的js文件,在文件里面创建一个json数据,如下图。

5、在test.html页面,给button绑定click点击事件,当按钮被点击时,使用getJSON方法获得ajax_json.js里面的json数据,同时通过function方法进行数据的输出。

6、在function方法里面,使用each()方法遍历json数据的内容,并使用append()方法把json里面的内容添加到p元素内。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-10-12
1、首先前台用Ajax,其中注意dataType一定要选择json方式,Action成功返回给页面的Json内容是这样的[{"number":"V006","names":"LiLei"}],可见comment['names']对应"names":"LiLei",comment['number']对应"number":"V006"。

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

$.ajax({
type: "post",
url:'apply/mystudent.action?',
cache: false,
dataType : "json",
success: function(data){

$.each(data, function(commentIndex, comment){

alert("姓名"+ comment['names']);

alert("学号"+comment['number']);

});
}
});

2、Ajax的URL指向在java的action中mystudent方法,返回的list其实是一个对象Student,包括了names和nunmber字段

?

1
2
3
4
5
6
7

public String mystudent() throws Exception{
List list=priceService.query();//调用接口实现类

this.jsonUtil(list);

return null;本回答被提问者采纳
相似回答