Jquery Ajax前后台交互

js代码片段:

var formName = 'formUsers', form = document.forms[formName];
var isValid = false;
function validateForm() {
     
var isValid = userDept(form.chname.value,form.deptName.value,form.parentId.value);
if(isValid){
            ajaxSubmitForm(formName,2000);
        }
}
function ajaxSubmitForm(formId, time) {
     
        postForm(true, formId, time);
}
function userDept(chname,deptName,value){
     
        var isValid=false;
        $.ajax({
            type : "POST",
            url : "${basePath}userManageController/selectUserByDeptId.do",
            async : false,
            data: {deptId: value,chname:chname},
            success : function(data) {
     
                if (data=='0') {
                    showMsg(deptName+'部门中已有该员工!');
                    return false;
                }else{
                    isValid=true;
                    return true;
                }
            },
            dataType : "json"
        });
        return isValid;
    }

html代码片段:

id="formUsers" name="formUsers" action="${basePath}/userManageController/addUsers.do" method="post"> "hidden" id="id" name="id" value="${users.id}" /> <div class="row">

class="label"> class="star">*员工编号:

"text" name="usercode" id="usercode" value="${users.usercode}" class="input" /> div> <div class="row">

class="label"> class="star">*姓名:

"text" name="chname" id="chname" value="${users.chname}" class="input" /> div> <div class="operations"> "javascript:;" class="operation btn_close" onclick="closeDiv(frameElement);">关闭 "javascript:;" class="operation btn_confirm" onclick="validateForm();">提交 div>

Controller代码:

@RequestMapping("/userManageController/selectUserByDeptId")
    public void selectUserByDeptId(HttpServletResponse response, String deptId,String chname) throws IOException {
        String result = "";
        try {
            result = userManageBo.selectUserByDeptId(deptId,chname);

            if(result.equals("")){
                response.getWriter().print("1");//相同部门下没有相同用户名
            }else{
                response.getWriter().print("0");//相同部门下有相同用户名
            }



        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().print("1");//相同部门下没有相同用户名
        }finally{
            response.getWriter().close();
        }
    }

你可能感兴趣的:(Jquery,ajax)