jquery ajax 传递js对象到后台

第一种:通过struts接收

(function ($) {
        $("#btnsave").click(function () {            
            var params = {};            
                params["gaglid.dwfd"] = $("#dwfd").val();                 
                params["gagl.gamc"] = $("#gamc").val();    
<%--                console.log($.param(params,true));--%>
<%--                console.log(decodeURIComponent($.param(params)));--%>
<%--                console.log(encodeURI(encodeURI(decodeURIComponent($.param(params)))));--%>			
				console.log($.param(params,true));
            var jqxhr = $.ajax({
                url: "save1Gagl.action?_=" + new Date().getTime(),
                type: "post",               
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                data: encodeURI($.param(params,true)),     //ajax传数组参数给struts2自动接收
                dataType: "text"
            });
            jqxhr.done(function (data) {            	
                alert(data);
            });
        });
    })($);

后台定义gaglid和gagl

public void save1()
	{
		try {
		if(null==gaglid||null==gaglid.getBh()||this.gaglid.getBh().equals(""))
		{
			this.gaglid.setBh(this.getbhmethod());
		}
		gagl.setId(gaglid);
		gagl.setGamc(URLDecoder.decode(gagl.getGamc(), "utf-8"));
	
		
//		gagl_Ithc_Server.saveOrUpdate(gagl);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/x-www-form-urlencoded; charset=utf-8");
		PrintWriter out;
			out = response.getWriter();
			out.write("success");
			out.flush();
			out.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

  

第二种:手动接收

//把要保存的个案,流调,送检单,传染病整合成一个js对象
			var gamerge={};
			gamerge.gagl=gamodel;
			gamerge.lxbdc=lxbdcmodel;
			gamerge.listsjdj=objs;
			gamerge.listcrbbg=crbbgobjs;			
			var jqxhrga = $.ajax({
	        	url: "save2AjaxGamerge.action?_=" + new Date().getTime(),
	            type: "post",
	            contentType: "application/json;charset=utf-8",
	            data:escape(encodeURIComponent(JSON.stringify(gamerge))), //把js对象转换成json  JSON.stringify(objs)
	            dataType: "text"
	        });
	  		jqxhrga.done(function (data) {	  			
	  			alert(data);
	  			window.opener.location.href=window.opener.location.href;   
	  			window.close();	  
	  			
	        });
	  		jqxhrga.fail(function(e){
	        	alert('保存个案出错'+e);
	        });

 后台手动接收

HttpServletRequest req=ServletActionContext.getRequest();
		try {
			req.setCharacterEncoding("utf-8");
			String t=readJSONStringFromRequestBody(req);
			t=java.net.URLDecoder.decode(java.net.URLDecoder.decode(t,"utf-8"),"utf-8");			

			//处理时间
			GsonBuilder gsonBuilder = new GsonBuilder();
		    gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
		        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		        public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
		                throws JsonParseException {
		            try {
		                return df.parse(json.getAsString());
		            } catch (ParseException e) {
		                return null;
		            }
		        }
		    });	
 Gson gson = gsonBuilder.create();
		    GaLdSjCrb galdsjcrb=gson.fromJson(t, new TypeToken<GaLdSjCrb>() {  
                }.getType());
String tsxx="保存成功!";

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/x-www-form-urlencoded; charset=GBK");
PrintWriter out = response.getWriter();
out.write(tsxx);
out.flush();
out.close();

  

 

posted on 2013-09-03 17:19  叶子绿了  阅读(2259)  评论(0编辑  收藏  举报

导航