前台javascript写法
function checkDimension(){ var startDate = document.getElementById("advInfoVo.startDate").value; var endDate = document.getElementById("advInfoVo.endDate").value; // 频道 var channelId = document.getElementById("selectedChannelId"); var channelIds = ""; for(var i=0; i<channelId.options.length; i++){ channelIds = channelIds + "," + channelId.options[i].value; } // 时间段 var showtime = document.getElementsByName("advInfoVo.timeFlag"); var showtimes = ""; for(var i=0; i<showtime.length; i++){ if(showtime[i].checked == true){ showtimes = showtimes + "," + showtime[i].value; } } // 地域 var areaId = document.getElementById("select2"); var areaIds = ""; for(var i=0; i<areaId.options.length; i++){ areaIds = areaIds + "," + areaId.options[i].value; } // 频次 var frequency = document.getElementById("advInfoVo.frequency"); var frequencys = ""; for(var i=0; i<frequency.options.length; i++){ frequencys = frequency.options[i].value; } // ajax后台校验 var url = "checkDimension?channelIds=" + channelIds + "&showtimes=" + showtimes + "&areaIds=" + areaIds + "&startDate=" + startDate + "&endDate=" + endDate + "&frequency=" + frequencys; if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } if (req) { req.open("POST",url,true); //指定回调函数为callback req.onreadystatechange = dimensionCallback; req.send(null); } }
回调函数
//回调函数 function dimensionCallback() { // 获取到工程路径 var localObj = window.location; var contextPath = localObj.pathname.split("/")[1]; var basePath = localObj.protocol+"//"+localObj.host+"/"+contextPath; if (req.readyState == 4) { if (req.status == 200) { var result = req.responseText.split(","); // parseMessage(); //解析XML文档 if(result[0] == ''){ adform.submit(); } else { document.getElementById("check_result_id").value = result[0]; document.getElementById("errorFile").href = basePath + "/downLoadFile?errorPath=" + result[1]; // 显示错误信息域 $('.whiteOverlay').show(); $("#dimensionInfo").show(); } } else { alert("不能得到描述信息:" + req.statusText); } } }
后台java代码
public void checkDimension() throws Exception { advInfoVo = new AdvInfoVo(); ActionContext context = ActionContext.getContext(); HttpServletRequest request = (HttpServletRequest) context .get(ServletActionContext.HTTP_REQUEST); HttpServletResponse response = (HttpServletResponse) context .get(ServletActionContext.HTTP_RESPONSE); String channelIds = request.getParameter("channelIds"); String showtimes = request.getParameter("showtimes"); String areaIds = request.getParameter("areaIds"); String startDate = request.getParameter("startDate"); String endDate = request.getParameter("endDate"); String frequency = request.getParameter("frequency"); advInfoVo.setChannelId(channelIds); advInfoVo.setAreaId(areaIds); advInfoVo.setTimeFlag(showtimes); advInfoVo.setStartDate(startDate); advInfoVo.setEndDate(endDate); advInfoVo.setFrequency(frequency); // 调用维度校验方法 List resultList = adService.checkAdDimension(advInfoVo); // 设置编码格式 response.setContentType("text/html;charset=utf-8"); // 将数据写到请求,返回前台 response.getWriter().write((String) resultList.get(1) + "," + (String) resultList.get(0)); }此方法最后2行代码是主要的(主要2种方式,1.返回文本、2.返回XML)