A simple Ajax class library and usage instance analysis, ajax class library instance analysis
This document describes a simple Ajax class library and its usage. We will share this with you for your reference. The details are as follows:
Ajax. js
Function Ajax (recvType) {var aj = new Object (); aj. recvType = recvType? RecvType. toUpperCase (): 'html' // html xml aj.tar getUrl = ''; aj. sendString = ''; aj. resultHandle = null; aj. createXMLHttpRequest = function () {var request = false; // the XMLHttpRequest in the window object is non-IE, including (IE7, IE8) if (window. XMLHttpRequest) {request = new XMLHttpRequest (); if (request. overrideMimeType) {request. overrideMimeType ("text/xml");} // The ActiveXObject attribute in the window object is IE} else if (window. activeXObject) {var ve Rsions = ['Microsoft. XMLHTTP ', 'msxml. XMLHTTP ', 'msxml2. XMLHTTP.7.0 ', 'msxml2. XMLHTTP.6.0 ', 'msxml2. XMLHTTP.5.0 ', 'msxml2. XMLHTTP.4.0 ', 'msxml2. XMLHTTP.3.0 ', 'msxml2. XMLHTTP ']; for (var I = 0; I <versions. length; I ++) {try {request = new ActiveXObject (versions [I]); if (request) {return request ;}} catch (e) {request = false ;}} return request;} aj. XMLHttpRequest = aj. createXMLHttpRequest (); aj. processHandle = f Unction () {if (aj. XMLHttpRequest. readyState = 4) {if (aj. XMLHttpRequest. status = 200) {if (aj. recvType = "HTML") aj. resultHandle (aj. XMLHttpRequest. responseText); else if (aj. recvType = "XML") aj. resultHandle (aj. XMLHttpRequest. responseXML) ;}}} aj. get = function (targetUrl, resultHandle) {aj.tar getUrl = targetUrl; if (resultHandle! = Null) {aj. XMLHttpRequest. onreadystatechange = aj. processHandle; aj. resultHandle = resultHandle;} if (window. XMLHttpRequest) {aj. XMLHttpRequest. open ("get", aj.tar getUrl); aj. XMLHttpRequest. send (null);} else {aj. XMLHttpRequest. open ("get", aj.tar getUrl, true); aj. XMLHttpRequest. send () ;}} aj. post = function (targetUrl, sendString, resultHandle) {aj.tar getUrl = targetUrl; if (typeof (sendString) = "object") {var Str = ""; for (var pro in sendString) {str + = pro + "=" + sendString [pro] + "&" ;}aj. sendString = str. substr (0, str. length-1);} else {aj. sendString = sendString;} if (resultHandle! = Null) {aj. XMLHttpRequest. onreadystatechange = aj. processHandle; aj. resultHandle = resultHandle;} aj. XMLHttpRequest. open ("post", targetUrl); aj. XMLHttpRequest. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); aj. XMLHttpRequest. send (aj. sendString) ;}return aj ;}
Usage:
<Script type = "text/javascript"> var ajax = Ajax (); // instantiate an object. The default value is HTTP. If XML is input, returns the XML Object // Ajax get method instructions/*** function (targetUrl, resultHandle) * @ param string targetUrl to pass the previous URL address * @ param string resultHandle callback function, optional */ajax. get ('test. php? Name = liruxing & email = liruxing1715@sina.com ', function (data) {eval ("var obj =" + data); alert (obj. name); alert (obj. email) ;}); // Ajax post method instructions/*** function (targetUrl, sendString, resultHandle) * @ param string targetUrl: Pass the previous URL address * @ param string sendString parameter value * @ param string resultHandle callback function, optional */ajax. post ('test. php ', 'name = liruxing & email = liruxing1715@sina.com', function (data) {eval ("var obj =" + data); alert (obj. name); alert (obj. email);}) // The second format of post. The second parameter is ajax in Json format. post ('test. php ', {name: 'lilu Star', email: 'ruxing1715 @ sina.com'}, function (data) {eval ("var obj =" + data); alert (obj. name); alert (obj. email) ;}) </script>