Tool: ajaxutil. js
VaR ajaxutil = {// basic options: {method: "Get", // default submitted method, get posturl: "", // Request Path requiredparams :{}, // request parameter type: 'text', // type of returned content, text, XML, jsoncallback: function () {}// callback function required }, // create the XMLHTTPRequest object createrequest: function () {var XMLHTTP; try {XMLHTTP = new activexobject ("msxml2.xmlhttp"); // IE6 or a later version} catch (E) {try {XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); // IE6 or earlier} c Atch (e) {try {XMLHTTP = new XMLHttpRequest (); If (XMLHTTP. overridemimetype) {XMLHTTP. overridemimetype ("text/XML") ;}} catch (e) {alert ("your browser does not support Ajax") ;}}return XMLHTTP ;}, // set the basic options setoptions: function (newoptions) {for (VAR pro in newoptions) {This. options [pro] = newoptions [pro] ;}}, // format the request parameter formateparameters: function () {var paramsarray = []; var Params = This. options. params; For (VAR pro in para MS) {var paramvalue = Params [pro]; paramsarray. push (Pro + "=" + paramvalue);} return paramsarray. join ("&") ;}, // readystatechange: function (XMLHTTP) {// obtain the returned value var returnvalue; If (XMLHTTP. readystate = 4 & XMLHTTP. status = 200) {Switch (this. options. type) {Case "XML": returnvalue = XMLHTTP. responsexml; break; Case "JSON": var jsontext = XMLHTTP. responsetext; If (jsontext) {returnvalue = eval ("(" + Jsontext + ")");} break; default: returnvalue = XMLHTTP. responsetext; break;} If (returnvalue) {This. options. callback. call (this, returnvalue);} else {This. options. callback. call (this) ;}}, // send the Ajax request: function (options) {var ajaxobj = This; // set the parameter ajaxobj. setoptions. call (ajaxobj, options); // create the XMLHTTPRequest object var XMLHTTP = ajaxobj. createrequest. call (ajaxobj); // sets the callback function XMLHTTP. onreadystatechange = f Unction () {ajaxobj. readystatechange. call (ajaxobj, XMLHTTP) ;}; // format the parameter var formateparams = ajaxobj. formateparameters. call (ajaxobj); // Request Method VaR method = ajaxobj. options. method; var url = ajaxobj. options. URL; If ("get" = method. touppercase () {URL + = "? "+ Formateparams;} // creates a connection to XMLHTTP. open (method, URL, true); If ("get" = method. touppercase () {// send the request XMLHTTP. send (null);} else if ("Post" = method. touppercase () {// If the request is submitted by post, set the request header information XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded"); // send the request XMLHTTP. send (formateparams );}}};
A simple example:
Function finduser () {var userid = $ ("userid "). value; If (userid) {ajaxutil. request ({URL: "Servlet/userjsonservlet", Params: {ID: userid}, type: 'json', callback: process}) ;}} function process (JSON) {If (JSON) {$ ("ID "). innerhtml = JSON. ID; $ ("username "). innerhtml = JSON. username; $ ("Age "). innerhtml = JSON. age;} else {$ ("MSG "). innerhtml = "user does not exist"; $ ("ID "). innerhtml = ""; $ ("username "). innerhtml = ""; $ ("Age "). innerhtml = "" ;}} function $ (ID) {return document. getelementbyid (ID );}