Share your own Ajax package

Source: Internet
Author: User

I have been working for two years. Because I have carefully studied the translation version of professional JavaScript For Web developers, I have always thought that my Js level is acceptable. Therefore, I am not familiar with many JS frameworks at work, although I have also studied these frameworks, I think they are indeed very powerful. When I encountered something in my work, I made a simple implementation. The Ajax package mainly came from professional JavaScript For Web developers, but I did some processing, it is sufficient to cope with your own work needs.

 

First, demonstrate how to call

// Define the callback function handleresponse (XMLHTTP) {If (XMLHTTP. readystate = 4) {var response = XMLHTTP. responsetext; // write your own handler here }}function checktext (OBJ) {var url = ".. /ashx/test. ashx "; customajax (" get ", URL, [" title "], [obj. value], handleresponse );}

Description: mamajax is the encapsulated call method provided externally. The first parameter is the data submission method, get or post, the second parameter is the submitted address, and the third and fourth parameters are arrays, is to submit data, the first number of Assembly keys, and the last value must correspond; the last one is the callback function, which is defined by yourself.

Calling is not too troublesome. It may not seem very object-oriented, and it is not quite used to it.

The code below:

// Create the XMLHTTP object function createxmlhttp () {var XMLHTTP; If (window. activexobject) {var arrsignature = ["msxml2.xmlhttp. 6.0 "," msxml2.xmlhttp. 5.0 "," msxml2.xmlhttp. 4.0 "," msxml2.xmlhttp. 3.0 "," msxml2.xmlhttp "," microsot. XMLHTTP "]; for (VAR I = 0; I <arrsignature. length; I ++) {try {XMLHTTP = new activexobject (arrsignature [I]); Return XMLHTTP;} catch (oexception) {} Throw new error ("MSXML is not installed on y Our IE. ");} else if (window. XMLHttpRequest) {XMLHTTP = new XMLHttpRequest (); Return XMLHTTP ;}} function addgetparam (Surl, sparamname, sparamvalue) {Surl + = (Surl. indexof ("? ") =-1? "? ":" & "); Surl + = escape (sparamname) +" = "+ escape (sparamvalue); Return Surl;} function addpostparam (sparams, sparamname, sparamvalue) {sparams + = (sparams. length> 0? "&": ""); Sparams + = escape (sparamname) + "=" + escape (sparamvalue); Return sparams ;}// main method function customajax (stype, surl, arrparamname, arrparamvalue, funstatechange) {var XMLHTTP = createxmlhttp (); var sparams = ""; if (arrparamname instanceof array & arrparamvalue instanceof array) {If (arrparamname. length = arrparamvalue. length) {If (stype. tolowercase () = "get") {sparams = NULL; For (VAR I = 0; I <arrparamname. length; I ++) Surl = addgetparam (Surl, arrparamname [I], arrparamvalue [I]);} else if (stype. tolowercase () = "Post") {for (VAR I = 0; I <arrparamname. length; I ++) sparams = addpostparam (sparams, arrparamname [I], arrparamvalue [I]) ;}} else {Throw new error ("Two Parma array's length is not same. ") ;}} else {Throw new error (" the third and fourth Param must be array. ");} XMLHTTP. open (stype. touppercase (), Surl, true); If (funstatechange instanceof function) {XMLHTTP. onreadystatechange = function () {funstatechange (XMLHTTP) ;}} else {Throw new error ("the last Param must be function. ");} XMLHTTP. send (sparams );}

There are so many, but one thing is to say

["Msxml2.xmlhttp. 6.0", "msxml2.xmlhttp. 5.0", "msxml2.xmlhttp. 4.0", "msxml2.xmlhttp. 3.0", "msxml2.xmlhttp", "microsot. XMLHTTP"]

On browsers that support ie standards, some computers may be capable of loading msxml2.xmlhttp. 6.0 prompt, because your browser may not have loaded this control; if you cannot tolerate this prompt on your website on your computer, delete the detection of this version from this array. Normally, when you delete the version to 4.0, the IE browser will not receive any loading prompt. If you are not at ease, you can still Delete the remaining microsot. XMLHTTP, where the jquery-1.3.2 is like this, the following is the jquery code of this version:

xhr:function(){return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();},

Of course, if you suspect that the above creation process is too cumbersome, you can also use jquery.

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.