ajax详细解说(含jsp的高级特性)

来源:互联网 发布:mac无法接收打包文件 编辑:程序博客网 时间:2024/06/04 19:48

<script src="script/client_validate.js"></script>  <script type="text/javascript">   function validateForm(form) {    var msg = "";    if (trim(form.clientId.value).length == 0) {     msg+= "分销商代码不能为空!\n";    }    if (trim(document.getElementById("clientIdSpan").innerHTML) != "") {     msg+="分销商代码已经存在!\n";    }    if (trim(form.clientName.value).length == 0) {     msg+= "分销商名称不能为空!";     }    if (msg != "") {       alert(msg);     return false;      }    return true;   }      var xmlHttp;       function createXMLHttpRequest() {    //表示当前浏览器不是ie,如ns,firefox    if(window.XMLHttpRequest) {     xmlHttp = new XMLHttpRequest();    } else if (window.ActiveXObject) {     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");    }   }

   function validateClientId(field) {    if (trim(field.value).length != 0) {     //创建XMLHttpRequest     createXMLHttpRequest();    //jsp的高级中都配这个<base href="<%=basePath %>">因为这是相当于根路径,你所有的都是资源都是从根开始找的,不用再写../../这种相对路径     var url = "<%=basePath%>servlet/ClientIdValidateServlet?clientId=" + trim(field.value) + "&timestamp=" + new Date().getTime();     xmlHttp.open("GET", url, true);     //方法地址,处理完成后自动调用,回调     xmlHttp.onreadystatechange=function() { //匿名函数      if(xmlHttp.readyState == 4) { //Ajax引擎初始化成功       if (xmlHttp.status == 200) { //http协议成功        if (trim(xmlHttp.responseText) != "") {//xmlHttp.responseText就是服务器端response.getWriter().print("分销商代码已经存在!");         document.getElementById("clientIdSpan").innerHTML = "<font color='red'>" + xmlHttp.responseText + "</font>";        }else {         document.getElementById("clientIdSpan").innerHTML = "";        }       }else {        alert("请求失败,错误码=" + xmlHttp.status);       }      }     };     //将参数发送到Ajax引擎     xmlHttp.send(null);    }else {     document.getElementById("clientIdSpan").innerHTML = "";//只是为输入为空是清空写入内容    }   }  </script>

原创粉丝点击