1、返回json字符串:
/** 将一个字符串输出到浏览器 */ protected void writeJson(String json) { PrintWriter pw = null; try { servletResponse.setContentType("text/plain;charset=UTF-8"); pw = servletResponse.getWriter(); pw.write(json); pw.flush(); pw.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (pw != null) { pw.close(); } } }
2、通过eval将返回的json字符串转换成json对象:
$.ajax({ data:{ "shipmmsi":shipmmsi, "shipname":shipname }, url : "shipbk/findShipMMSIAndName.do", async : true, type : "POST", success : function(data) { var ships = eval('(' + data + ')'); $("#bindShipmmsiDiv table tbody").html(""); if(ships!=null){ if(ships.length){ $("#bindShipmmsiDiv").show(); var trs=""; for(var i=0;i" +ships[i].mmsi+"< td> "+ships[i].vesselName+""; } $("#bindShipmmsiDiv table tbody").append(trs); //给tr注册点击事件 $("#bindShipmmsiDiv table tbody tr").click(function(){ $(this).addClass('select_tr').siblings().removeClass('select_tr'); }); $("#bindShipmmsiDiv table tbody tr").dblclick(function(){ fillShipMMSIAndName(this); $("#bindShipmmsiDiv").hide(); }); } } } }); 3、通过jquery的 $("form").serialize() 可以将form表单的数据序列化后提交到后台,因此通过ajax可以操作form表单并处理返回的数据。
$.ajax({ url : 'deliveryWarrant/update.do', data : $('#myform').serialize(), type : "POST", success : function(data) { var res = eval('(' + data + ')'); if (res && res.success == true) { alert(res.message); location.href="/godownWarrant/findToDeliveryWarrant.do?godownWarrant.code="+$("#myform input[name=godownWarrant\\.code]").val(); } else { alert(res.message); } }});4、防止乱码的处理方法:
jsp页面:charset:utf-8
servlet:utf-8filter:utf-8在PrintWriter out = response.getWriter()之前加一句response.setCharacterEncoding("UTF-8")就可以解决乱码的问题。但是得记住一定要放在声明PrintWwrite之前。转载地址:http://ivlhl.baihongyu.com/
你可能感兴趣的文章微信公众平台java开发具体解释(project代码+解析)查看>>Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config.查看>>【SQL】sql版Split函数。用于拆分字符串为单列表格查看>>【BZOJ】1044: [HAOI2008]木棍分割(二分+dp)查看>>哈佛经济学家关于工作效率的意外发现查看>>维基百科一年烧四千多万美元 太会花钱?才不是呢!查看>>软件管理,软件生命周期,软件过程名词解释查看>>[Spring MVC] - SpringMVC的各种参数绑定方式查看>>apk签名方法查看>>java.io.IOException: ORA-22920: 未锁定含有 LOB 值的行查看>>用一条sql取得第10到第20条的记录查看>>《悲惨世界》--[法]雨果查看>>分页骨架查看>>IOS--UIDatePicker 时间选择器 的使用方法详细查看>>ORACLE系统表大全查看>>#研发解决方案介绍#基于持久化配置中心的业务降级查看>>设计模式--建造者(Builder)模式查看>>高效方法势图查看>>cocos设计模式之二段构建模式查看>>PHP面向对象的基本写法(区别于java)查看>>