ajax前后台上下文

前端之家收集整理的这篇文章主要介绍了ajax前后台上下文前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
========后台前台就是一个标准的String json串,真正到了前台变成对象会对象数组=========== public String queryCutOverOrderById() { String id=model.getId(); CutOverOrder a =new CutOverOrder(); List<CutOverOrder> tr = new ArrayList<CutOverOrder>(); tr =this.getService().queryCutOverOrderById(id); if(tr.size()>0){ a=tr.get(0) ; JsonConfig config = new JsonConfig(); config.setExcludes(new String[]{"processLogs"}); String json = JSONArray.fromObject(a,config).toString(); HttpServletResponse response = ServletActionContext.getResponse();; response.setCharacterEncoding("utf-8"); //ServletOutputStream out = null; BufferedWriter write=null; try { write = new BufferedWriter(new OutputStreamWriter(response .getOutputStream(),"UTF-8")); write.write(json); // out = response.getOutputStream(); // out.write(json.getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { try { //out.flush(); //out.close(); write.flush(); write.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } else{ return null; } } 前台: $.ajax({ type:'post',url:url,async:false,data:null,dataType:'json',success:function(data){ var a = eval(data); if(data){ for(var i=0;i<a.length;i++){ s[i]=a[i].stage; t[i]=a[i].recordTime; inc[i]=a.cnName; } } },error:function(){ //$.message("失败"); } }); 前台后台: $.ajax({ type:'post',url:'<%=csgui%>',data:'dataArraySave='+JSON.stringify(dataArraySave),success:function(data){ $.message("成功"); $("#list2").grid('reload'); },error:function(){ $.message("失败"); } }); @Transactional public void getJson() { HttpServletRequest request = ServletActionContext.getRequest(); String dataArraySave = request.getParameter("dataArraySave"); String[] a = JsonUtil.jsonStrToStringArray(dataArraySave); Params[] b= (Params[]) JsonUtil.jsonStrToArray(dataArraySave,Params.class); List<Params> c = new ArrayList<Params>(); for(int i=0;i<b.length;i++){ c.add(b[i]); } this.getService().saveParams(c); } ===============前台后台可能会传一个list<object>,list<string[]这时要将他们转化为Sting作为参数传到后台>============ 另一种: String ui= request.getContextPath() + "/interface-cnfig!intesvae.json?smal="; function getj(){ var ids = []; var dataArraySave = [],json = ""; var sel = $("#list2").grid("option","selarrrow"); ids=$("#list2").grid( "getDataIDs"); $.each(sel,function(i){ dataArraySave.push($("#list2").grid("getRowData",sel[i])); }); textvar = JSON.stringify(dataArraySave); return textvar; } //这一步将list的记录(每一条一个json)转化为大的json数组 //[{"id":"3","csbs":" 666","csmc":" 666","cslx":" 666"},//{"id":"2","csbs":"66","csmc":" 66","cslx":" 66"},//{"id":"1","csbs":" //6","csmc":" 6","cslx":" 6"}] function f_new() { // window.execScript( 'window.varText='+""+';'); var uu='<%=ui%>'; //alert(uu); $("#search-form").dialog({ autoOpen : false,height : 'auto',width : 700,modal : true,buttons : { "保存" : function() { var a= getj(); var uui=uu + a; //alert(uui); //$("#form11").form("submit"); if ($("#form11").form("valid")) { $.ajax({ type : 'post',url : uui,data : $("#form11").form("formData",false),dataType : 'json',success : function(data) { $.message("操作成功!"+data.id); $('#list1').grid('reload'); },error : function() { $.message("操作失败!"); } }); } else { $.alert("未通过页面校验!") } },"关闭" : function() { $(this).dialog("close"); } } }); public String intesvae() { String id=""; HttpServletRequest request = ServletActionContext.getRequest(); id =this.getService().saveIntcfg(model); String a = request.getParameter("smal"); if(a!=""||a!="["+"]"){ Params[] b= (Params[]) JsonUtil.jsonStrToArray(a,Params.class); List<Params> c = new ArrayList<Params>(); for(int i=0;i<b.length;i++){ b[i].setJkid(id); c.add(b[i]); } this.getService().saveParams(c); } return id; } ================保存对应的form========= <div id="search-form" title="接口添加界面" style="display: none" > <cui:form id="form11" name="form" action="<%=ui %>"> <table height="100%"> <tr style="vertical-align: top; height:45px" > <td style="text-align: left; padding-left: 10px;" height="15"> <div style="float: left;padding-top: 8px;"><label>&nbsp;接口名称:</label></div> <div style="float: left;padding-top: 8px;"><cui:input id="interfacename" name="interfacename" placeholder="接口名称" value="" width="200" /></div> <div style="float: left;padding-top: 8px;"><label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数据类型:</label></div> <div style="float: left;padding-top: 8px;"><cui:comboBox id="datatype" name="datatype" width="200" value="" emptyText="请选择" data="combo_data"></cui:comboBox></div> </td> </tr> <tr style="vertical-align: top; height:45px"> <td style="text-align: left; padding-left: 10px;"> <div style="float: left;padding-top: 8px;"><label>&nbsp;接口地址:</label></div> <div style="float: left;padding-top: 8px;"><cui:input id="interfaceadRSS" name="interfaceadRSS" value="" width="200" /></div> <div style="float: left;padding-top: 8px;"><label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;接口类型:</label></div> <div style="float: left;padding-top: 8px;"><cui:comboBox id="interfacetype" name="interfacetype" width="200" value="" emptyText="请选择" data="combo_data_interface"></cui:comboBox></div> </td> </tr> <tr style="vertical-align: top; height:45px"> <td style="text-align: left; padding-left: 10px;"> <div style="float:left;"><label>&nbsp;接口厂商:</label> <!--<cui:comboBox id="getway" name="getway" required="true" width="200" value="" emptyText="请选择" data="combo_data_get"></cui:comboBox> --> <cui:input id="interfacefac" name="interfacefac" placeholder="接口厂商" value="" width="200" /> </div> <div style="float:left;padding-top: 8px;"><label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用户认证:</label> <cui:input id="confirm" name="confirm" placeholder="用户认证" value="" width="200" /></div> </td> </tr> <tr style="vertical-align: top" id="param"> <td> <div style="float:left;padding-top: 8px;"><label>&nbsp;&nbsp;&nbsp;是否同步:</label> </div> <div style="float:left;padding-top: 8px;"> <cui:radiolist id="sftb" column="4" name="sftb" value="none" data="radiolistdata" > </cui:radiolist> </div> </td> </tr> <tr style="vertical-align: top" id="param"> <td style="text-align: right; padding-left: 10px;" > <div style="float:left;"><label>&nbsp;参数类型:</label> </div> </td> </tr> <tr> <td style="float:left;"> <div style="float:left;"> <input type="button" class='btnview coral-btn-blue' id="add" value="添加行"></input> </div> </td> <td style="float:left;"> <div style="float:left;"> </div> <div style="float:left;"><input type="button" class='btnview coral-btn-blue' id="edite" value="编辑行"></input></div> <div style="float:left;"> <input type="button" class='btnview coral-btn-blue' id="saveEdite" value="保存编辑"></input> </div> </td> </tr> <tr> <td colspan="4"> <cui:grid id="list2" onComplete="gridoncomplete" datatype="local" multiselect="true"> <cui:gridCols> <cui:gridCol name="id" hidden="false" editable="true" align="center" width="80">序号</cui:gridCol> <cui:gridCol name="csbs" editable="true" align="center" width="160">参数标识</cui:gridCol> <cui:gridCol name="csmc" editable="true" align="center" width="160">参数名称</cui:gridCol> <cui:gridCol name="cslx" editable="true" align="center" width="160">参数类型</cui:gridCol> <!--<cui:gridCol name="cz" editable="true" align="center" width="120">操作</cui:gridCol> --></cui:gridCols> <cui:gridPager gridId="list2" /> </cui:grid> <!--<cui:grid id="list2" datatype="local" multiselect="true" rownumbers="true"> <cui:gridCols> <cui:gridCol name="id" editable="true" hidden="true" width="100">id</cui:gridCol> <cui:gridCol name="name" editable="true" width="80">员工帐号</cui:gridCol> <cui:gridCol name="src" editable="true" width="80">图片路径</cui:gridCol> <cui:gridCol name="age" editable="true" width="80">年龄</cui:gridCol> <cui:gridCol name="createtime" editable="true" edittype="date" width="80">时间点1</cui:gridCol> <cui:gridCol name="createtime1" editable="true" edittype="date" width="80">时间点2</cui:gridCol> </cui:gridCols> </cui:grid> --></td> </tr> </table> </cui:form> </div> 原文链接:https://www.f2er.com/ajax/164347.html

猜你在找的Ajax相关文章