A simple Ajax class library and usage instance analysis, ajax class library instance analysis

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.