Encapsulated Ajax Tool Library

Source: Internet
Author: User

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 );}

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.