<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">//<script language= "javascript" type= "Application/javascript" >//If the above code cannot be implemented completely, use this//how to instantiate IE browser//var h = new ActiveXObject ("Msxml2.xmlhttp");//var p = new ActiveXObject ("Microsoft.XMLHTTP");//non-IE browser//var f = new XMLHttpRequest ();//alert (P.getallresponseheaders ());var hx=false;//Defining Functionsfunction Test (user_id) {//common instantiation code for all browsers if(Window. XMLHttpRequest)//Non-IE IE7 versions and above are supported in forms other than IE{HX=NewXMLHttpRequest ();//if it is a non-ie browser then it will be instantiated//alert ("QQQ"); //If the instantiation succeeds, then the sentence is output } Else if(Window. ActiveXObject)//IE { Try{HX=NewActiveXObject ("Msxml2.xmlhttp");//instantiation of//alert ("QQQ2"); //If the instantiation succeeds, then the sentence will be output. } Catch(e) {alert (e); Try{HX=NewActiveXObject ("Microsoft.XMLHTTP");//instantiation of//alert ("Qqq3"); //If the instantiation succeeds, then the sentence will be output. } Catch(e) {alert (e); } }}//test whether the creation of XMLHttpRequest was successfulif(!HX) {Alert ("Create XMLHttpRequest Failed"); }Else{alert ("Create XMLHttpRequest Success"); } //The request type of an asynchronous request has two ways, one is get and the other is post two methods have their own way//Get modeAlert (NewDate (). GetTime ()); //1 Setting the URL for an asynchronous requestHx.open ("GET", "ajaxtest?userid=" + user_id + "&nocache =" +NewDate (). GetTime (),true);//("Request Type Get/post", URL, whether asynchronous True/false)//2 Setting the callback function event handlerHx.onreadystatechange = function ()//Anonymous Functions{GetResult ("1"); }//hx.onreadystatechange = GetResult; //give the function name of the callback function as an event handler to Hx.onreadystatechange//calling the requested sendHx.send (NULL);//Post when an asynchronous request is set when a request transfer parameter is required } //callback functionfunction GetResult (type)//function GetResult () { // //alert ("readyState =" +hx.readystate); if(Hx.readystate = = 4)//decide whether to complete { if(Hx.status = = 200)//determine if the server is complete, normal {//alert ("Callback message =" + Hx.responsetext);//the value returnedalert (hx.responsetext); alert (type); //alert ("Headers =" + hx.getallresponseheaders ()); //gets all header information for the returned function//alert ("server =" + hx.getresponseheader ("server")); //get Partial header information--Callback information } Else{alert ("Error Status Code =" + Hx.status + "status text message =" +hx.statustext); } } } /* Post mode//1 set the URL of the asynchronous request such as information Hx.open ("POST", "Ajaxtest", true); ("Request Type Get/post", URL, whether asynchronous true/false)//2 Set callback function event handler Hx.onreadystatechange = function ()//anonymous function {GetResult ("1"); }//Hx.onreadystatechange = GetResult; The function name of the callback function is used as an event handler to send the Hx.onreadystatechange//Call request//Post Hx.setrequestheader when an asynchronous request is set when a request routing parameter is required ( "Content-type", "application/x-www-form-urlencoded"); Post mode data transfer get method can be placed directly in the URL hx.send ("userid=" + user_id); }//Callback function GetResult (type) {////alert ("readyState =" +hx.readystate); if (hx.readystate = = 4)//Decide whether to complete {if (Hx.status = = 200)//Determine if the server is complete, normal {//Alert ("Callback information =" + Hx.responsetext); The value returned by alert (hx.responsetext);//alert ("Headers =" + Hx.getallresponseheade RS ()); Gets all header information for the returned function Alert ("server =" + hx.getresponseheader ("server")); Get partial header information-callback message} else {alert ("Error status Code =" + HX . Status + "status text message =" + Hx.statustext); } } }*/ //alert ("server =" + hx.getallresponseheaders ("server"));</script>User code:<input type= "text" onchange= "Test (This.value);"/></body>Ajax PackageCom.hanqi;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet Implementation class Ajaxtest*/@WebServlet ("/ajaxtest") Public classAjaxtestextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /*** Default constructor. */ Publicajaxtest () {//TODO auto-generated Constructor stub } /** * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response)*/ protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("UTF-8"); Response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html; Charset=utf-8 "); String userid= Request.getparameter ("userid"); if(UserID! =NULL&& Userid.trim (). Length () > 0) { //Find user Information if(Userid.equals ("ABC") {response.getwriter (). Append ("User code already exists!" "); } Else{response.getwriter (). Append ("User code does not exist, can be used!" "); } } Else{response.getwriter (). Append ("Please fill in correctly!" "); } response.getwriter (). Append ("Test Ajax"); Response.getwriter (). Append ("Served at:"). Append (Request.getcontextpath ()); } /** * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/ protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method Stubdoget (request, response); }}
ajaxtestA little supplement to 20151217--ajax