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
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
相关文章推荐
- JSON 体验JSON (一) jquery/AJAX 筒单例子
- jQuery/Ajax/PHP/Json 的一个综合例子
- jQuery/Ajax/PHP/Json 的一个综合例子
- jQuery、Ajax、PHP、Json的一个综合例子
- php+jquery+ajax+json简单小例子
- php+jquery+ajax+json简单小例子
- php+jquery+ajax+json简单小例子
- jquery的ajax方式调用json简单例子
- jquery的ajax返回json数据另外一个例子(不继承json-default)
- servlet+jquery+ajax+json 例子
- jQuery/Ajax/PHP/Json 的一个综合例子
- jQuery/Ajax/PHP/Json 的一个综合例子
- jQuery/Ajax/PHP/Json 的一个综合例子
- php+jquery+ajax+json简单小例子
- Ajax+JSON+JQuery小例子
- jQuery/Ajax/PHP/Json 的一个综合例子
- 企业开发中js,Jquery,Ajax和json格式的一个例子
- jQuery/Ajax/PHP/Json 的一个综合例子
- php+json+ajax+jquery小例子
- Ajax + JQuery + JSON + Struts使用例子