打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ajax 调用action方法 ,返回一个对象的list列表,调用成功后循环遍历
js主要代码:
function test(){
var url = "${pageContext.request.contextPath}/elecRoleAction!edit.action"; 
$.ajax({ 
url:url, 
type:'post', //数据发送方式 
dataType:'json', //接受数据格式 (这里有很多,常用的有html,xml,js,json) 
data:, //要传递的数据 
error: function(){ //失败 
alert('Error loading document'); 
}, 
success: function(msg){ //成功 
alert("服务器返回数据如下:");
for(var i = 0;i<msg.length;i++){
var mm = msg[i];
alert("userName is:"+mm.userName+"  password is:"+mm.pasd);
}
}); 
}
java主要代码:
public class ElecRoleAction extends BaseAction {
public void edit() throws IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 需要导入gson包;import com.google.gson.JsonObject;
JsonObject jsonObj = new JsonObject();
jsonObj.addProperty("userName", "hello");
jsonObj.addProperty("pasd", "12345");
JsonObject jsonObj2 = new JsonObject();
jsonObj2.addProperty("userName", "ajax");
jsonObj2.addProperty("pasd", "678910");
List<JsonObject> jsonList = new ArrayList<JsonObject>();
jsonList.add(jsonObj);
jsonList.add(jsonObj2);
String str = jsonList.toString();
out.println(str);
}
}

public  class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{

protected HttpServletRequest request = null;
protected HttpServletResponse response = null;  
//// 实现ServletResponseAware接口的setServletResponse方法,用于获得request对象
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
// 实现ServletResponseAware接口的setServletResponse方法,用于获得response对象
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
jQuery学习之:jqGrid表格插件——从servlet获得数据
JSONObject简介
javascript转换字符串为json对象的方法
JSONObject与JSONArray的使用
JAVA中使用JSON进行数据传递
JSON学习 - JavaScript - web - JavaEye论坛
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服