您的位置:首页 > Web前端 > JQuery

jquery - ajax - json 例子

2013-08-31 18:53 357 查看
----jsp:----

function fInit(){

$.ajax({

type: "post", //使用get方法访问后台

dataType: "json", //返回json格式的数据

url: "yhzcgl.do?method=GetGroup", //要访问的后台地址

data: "", //要发送的数据

complete :function(){}, //AJAX请求完成时

success: function(msg){ //msg为返回的数据,在这里做数据绑定

for (var i in msg){

$("#ssz").append("<option value="+msg[i].bh+">"+msg[i].mc+"</option>");

}

}

});

}



----java:----

//jquery-ajax

public void GetGroup(ActionMapping actionMapping, ActionForm actionForm,

HttpServletRequest httpServletRequest,

HttpServletResponse httpServletResponse) throws IOException,

ServletException {

try{

List lstGroup = groupBusiness.ShowUGroup(groupBean);

//将List转化为JSON

JSONArray json=JSONArray.fromObject(lstGroup);

//设置编码

httpServletResponse.setCharacterEncoding("gbk");

//写入到前台

httpServletResponse.getWriter().write(json.toString());

}catch(Exception e){

logger.error(e);

}

}

----需要的包:----

jquery-1.2.6.min.js

json-lib-2.3-jdk15.jar

commons-beanutils-1.7.0.jar

commons-httpclient-3.1.jar

commons-lang-2.3.jar

commons-logging-1.0.4.jar

commons-collections-3.1.jar

ezmorph-1.0.3.jar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: