怎么用ajax将后台List取出并在前台jsp页面做显示???
js:<script>functionshow(name){$.post("groupAction_getAllByGroup.action",{name:name},f...
js:
<script>
function show(name){
$.post("groupAction_getAllByGroup.action",{name:name}, function(data){
$(data.users).each(function(){
alert($(this).attr("username"));
});
});
}
</script>
后台action:
public String getAllByGroup() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
JSONArray jsonArray = new JSONArray();
JSONObject jsonobj = new JSONObject();
String name = request.getParameter("name");
List<User> users = dao.getAllByGroup(name);
User us = new User();
us.setUser_name("names");
users.add(us);
for(User user : users){
jsonobj.put("username", user.getUser_name());
jsonArray.add(jsonobj);
}
jsonobj = JSONObject.fromObject(jsonArray);
out.print(jsonobj);
out.flush();
out.close();
return null;
} 展开
<script>
function show(name){
$.post("groupAction_getAllByGroup.action",{name:name}, function(data){
$(data.users).each(function(){
alert($(this).attr("username"));
});
});
}
</script>
后台action:
public String getAllByGroup() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
JSONArray jsonArray = new JSONArray();
JSONObject jsonobj = new JSONObject();
String name = request.getParameter("name");
List<User> users = dao.getAllByGroup(name);
User us = new User();
us.setUser_name("names");
users.add(us);
for(User user : users){
jsonobj.put("username", user.getUser_name());
jsonArray.add(jsonobj);
}
jsonobj = JSONObject.fromObject(jsonArray);
out.print(jsonobj);
out.flush();
out.close();
return null;
} 展开
展开全部
你可以在当前的页面中添加一个<div>标签,如
<div id="container" ></div>
在用jquery发送ajax请求并返回结果后,拼装html代码,并将拼接后的结果放到div标签之间就可以了
<script>
function show(name){
$.post("groupAction_getAllByGroup.action",{name:name}, function(data){
var html="";
$(data.users).each(function(){
html+='.......'; //此处拼装要显示的html代码
});
$('#container').html(html); //将拼装后的代码嵌入到<div>标签内。
});
}
</script>
<div id="container" ></div>
在用jquery发送ajax请求并返回结果后,拼装html代码,并将拼接后的结果放到div标签之间就可以了
<script>
function show(name){
$.post("groupAction_getAllByGroup.action",{name:name}, function(data){
var html="";
$(data.users).each(function(){
html+='.......'; //此处拼装要显示的html代码
});
$('#container').html(html); //将拼装后的代码嵌入到<div>标签内。
});
}
</script>
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |