1.ajaxRequest.js
function createRequest(url,number) { HTTP_Request = false; if (window.XMLHttpRequest) { // 非IE浏览器 HTTP_Request = new XMLHttpRequest(); //创建XMLHttpRequest对象 } else if (window.ActiveXObject) { // IE浏览器 try { HTTP_Request = new ActiveXObject("Msxml2.XMLHTTP"); //创建XMLHttpRequest对象 } catch (e) { try { HTTP_Request = new ActiveXObject("Microsoft.XMLHTTP"); //创建XMLHttpRequest对象 } catch (e) {} } } if (!HTTP_Request) { alert("不能创建XMLHttpRequest对象实例!"); return false; } //调用返回结果处理函数 if(number==1) //回调函数 HTTP_Request.onreadystatechange = getResult; HTTP_Request.open('GET',url,true); //创建与服务器的连接 //true异步提交请求 //false同步提交请求 HTTP_Request.send(null); //发送请求:实体内容 } function getResult(){ //alert(n); //alert(HTTP_Request.responseText); //0初始化1正在加载2已加载3交互中4完成 if(HTTP_Request.readyState==4){ if(HTTP_Request.status=200){ document.getElementById("pid_tooltip").innerHTML=HTTP_Request.responseText; }else { alert("您所请求的页面错误!"); } } } function pid_tooltip_check(){ createRequest('Do?action=pid_tooltip_ask-receipt&pid='+document.getElementById("idnumber").value,1); }
当包含特殊字符时,如下处理:
createRequest('Do?action=pid_tooltip_ask-receipt&pid='+escape(document.getElementById("idnumber1").value),7);
Post请求方式:
HTTP_Request.open("POST","Do?action=pid_tooltip_ask-receipt",true); HTTP_Request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); HTTP_Request.send("pid="+escape(document.getElementById("idnumber1").value));
Servlet
/* * 身份证合法性检测 */ protected void pid_tooltip(HttpServletRequest request,HttpServletResponse response) throws servletexception,IOException { // Todo Auto-generated method stub String pid=request.getParameter("pid")==null?"":request.getParameter("pid");//身份证 String mes=""; //System.out.println(pid); //18位 if(pid.length()!=18){ //身份证必须是18位 //response.sendRedirect("ask-receipt.jsp?flag=121"); //mes="<font color=red>身份证必须是18位</font>"; mes="<font color=red>×</font>"; cf.pid_correct=false; } else{ PidCheck pc=new PidCheck(); if(pc.check_(pid)){ //身份证输入正确 //mes="<font color=green>身份证正确</font>"; mes="<font color=green>√</font>"; cf.pid_correct=true; }else{ //身份证输入有误 //mes="<font color=red>身份证有误!请重新输入</font>"; mes="<font color=red>×</font>"; cf.pid_correct=false; } } //System.out.println(cf.pid_correct); //System.out.println(mes); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control","no-store");//http1.1不缓存 response.setHeader("Pragma","no-cache");//http1.0不缓存 response.setDateHeader("Expires",0);//阻止从代理服务器获取 PrintWriter out=response.getWriter(); out.println(mes); //System.out.println(mes); out.flush(); out.close(); }