struts1 下的 jquery ajax 记录
JS 代码
function checkInternalTransaction(actionType,idNum) { var url = "/sales/checkInternalTransaction.do"; var checkMsg; $.ajax({ type : 'post', dataType : "text", url : url, data : "actionType="+actionType+"&idNum="+idNum, cache : false, async : false, success : function(msg) { checkMsg = msg; //msg 即前台返回的值,默认字符串类型,后台返回了 true 前台接收到的是字符串 "true" } }) return checkMsg; }
struts1 配置
<action path="/checkInternalTransaction" type="com.sinosoft.sales.ui.control.facade.UIInternalTransactionFacade" name="fm" scope="request"> </action>
后台代码
public class UIInternalTransactionFacade extends Action{ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DBInternalTransaction DBInternalTransaction = new DBInternalTransaction(); boolean resultBoolean = true; String actionType = request.getParameter("actionType"); String idNum = request.getParameter("idNum"); if(null==idNum||"".equals(idNum)){ response.getWriter().print(true); //直接调用该方法想前台返回值 return null; } if(actionType.equals("IDV")){ int count = DBInternalTransaction.checkIdvIdNum(idNum); resultBoolean = count==0?true:false; //未查询到数据,则校验通过,查询到数据则在页面弹出提示。 } if(actionType.equals("UNIT")){ int count = DBInternalTransaction.checkUnitIdNum(idNum); resultBoolean = count==0?true:false; //未查询到数据,则校验通过,查询到数据则在页面弹出提示。 } response.getWriter().print(resultBoolean); return null; //不需要跳转,返回null } }