XmlHttpRequest用法

function getDepartmentForSelect() {
            createXMLHttpRequest();
            var url = '../xxx.do';
            XMLHttpReq.open("GET", url, true);
            XMLHttpReq.onreadystatechange = handleDepartment;//指定响应函数
            XMLHttpReq.send(null);
        }
        function handleDepartment() {
            if (XMLHttpReq.readyState == 4) { // 判断对象状态
                if (XMLHttpReq.status == 200) {
                    // 信息已经成功返回,开始处理信息
                    onreadyDepartment();
                } else { //页面不正常
                    window.alert("您所请求的页面有异常。");
                }
            }
        }
        //创建XMLHttpRequest对象
        function createXMLHttpRequest() {
            if (window.XMLHttpRequest) { //Mozilla 浏览器
                XMLHttpReq = new XMLHttpRequest();
            }
            else if (window.ActiveXObject) { // IE浏览器
                try {
                    XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try {
                        XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {
                    }
                }
            }
        }
        function onreadyDepartment() {
            var xmlResponse = XMLHttpReq.responseXML;
            var departments = xmlResponse.getElementsByTagName("department");
            if (departments.length == 0) {
                return;
            }
            var queryDepartment = document.getElementById("departmentHidden").value;
            var objSelect = document.getElementById("department");
            var departmentArray = departments[0].firstChild.text.split(",");
            var varItemHead = new Option();
            objSelect.options.add(varItemHead); //头部
            for (var i = 0; i < departmentArray.length; i++) {
                var varItem = new Option(departmentArray[i], departmentArray[i]);
                if (queryDepartment == departmentArray[i]) {
                    varItem.selected = true;
                }
                objSelect.options.add(varItem);
            }
        }
        getDepartmentForSelect();
<input type="hidden" value="<%=queryDepartment%>" id="departmentHidden"/>
<select name="department" id="department" style="width:90px;"></select>
String returnDepartment = "";
        List<String> departmentList = DepartmentPartDAO.getDepartmentList();
        StringBuilder departmentSB = new StringBuilder();
        for (String str:departmentList){
            departmentSB.append(str+",");
        }
        if(departmentSB.length()>0){
            returnDepartment = departmentSB.substring(0,departmentSB.length()-1);
        }
        PrintWriter out;
        try {
            response.setContentType("text/xml;charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            out = response.getWriter();
            out.println("<response>");
            out.println("<department>" + returnDepartment + "</department>");
            out.println("</response>");
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
request.setAttribute("queryDepartment", InterceptPartQueryForm.getDepartment());

相关推荐