Get XMLHTTPRequest object
Function XMLHttpRequest () {var XMLHTTP; try {XMLHTTP = new XMLHttpRequest (); // Firefox, opera 8.0 +, Safari} catch (E) {try {XMLHTTP = new activexobject ("msxml2.xmlhttp"); // Internet Explorer} catch (e) {try {XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); // Internet Explorer} catch (e) {}} return XMLHTTP ;}
Ajax ProcessingCode
Window. onload = function () {document. getelementbyid ("OK "). onclick = function () {// create the XMLHTTPRequest object var XMLHTTP = XMLHttpRequest (); // register the listener XMLHTTP. onreadystatechange = function () {If (XMLHTTP. readystate = 4) {// 200 indicates OK, and 304 indicates that if (XMLHTTP. status = 200 | XMLHTTP. status = 304) {// receives the data returned by the server var DATA = XMLHTTP. responsetext; alert (data) ;}}// enable the connection to the server and add timestamp to eliminate the cache XMLHTTP. open ("get ",".. /testservlet? Timestamp = "+ new date (). gettime () +" & Pam = 18 ", true); // send data to the server XMLHTTP. Send (null );}}
Server-side processing code
Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = response. getwriter (); system. out. println ("Pam =" + request. getparameter ("Pam"); // the server sends data to the client out. println ("success") ;}