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(); }原文链接:https://www.f2er.com/ajax/163713.html