Publish a simple and compact Ajax operation class

Source: Internet
Author: User
I want to publish a self-compiled Ajax framework (with examples. However, I read the article mentioned in the previous sentence. Article Some of the following comments are a little scared. Therefore, it must be affirmed that this is a simple and effective Ajax operation class prepared by the author based on network resources (the so-called "Framework" is not worth the candle ), for small projects that are not too large in size and do not want to reference third-party Ajax libraries, Ajax operation requirements are not too high or too frequent. below is enough to meet the requirements. Code
// Encapsulate the myajaxobj class of XMLHTTPCode
VaR Myajaxobj =   New Object ();

Myajaxobj. reqlist = [];

// Create an XMLHTTP object that is compatible with different browsers
Function Getxmlhttp (){
VaR XMLHTTP =   False ;
VaR Arrsignatures = [ " Msxml2.xmlhttp. 5.0 " , " Msxml2.xmlhttp. 4.0 " ,
" Msxml2.xmlhttp. 3.0 " , " Msxml2.xmlhttp " ,
" Microsoft. XMLHTTP " ];
For ( VaR I =   0 ; I < Arrsignatures. length; I ++ ){
Try {
XMLHTTP =   New Activexobject (arrsignatures [I]);
Return XMLHTTP;
}
Catch (Oerror ){
XMLHTTP =   False ; // Ignore
}
}
// Throw new error ("MSXML is not installed on your system .");
If ( ! XMLHTTP &&   Typeof XMLHttpRequest ! =   ' Undefined ' ){
XMLHTTP =   New XMLHttpRequest ();
}
Return XMLHTTP;
}

/* Encapsulate requests sent from XMLHTTP to the server
URL: Request Path to the server; Method: Request method, that is, get/post; *** callback: when the server returns a successful result, called functions (similar to C # callback functions )***
Data: The data contained in the request to the server; urlencoded: whether the URL is encoded; cached: whether to use the cache; callbackerror; the function called when the server returns an error
*/
Myajaxobj. Send =   Function (URL, method, callback, Data, urlencoded, cached, callbackerror ){
VaR REQ = Getxmlhttp (); // Get an XMLHTTP instance

// Called when the Request status of XMLHTTP changes (core processing function)
Req. onreadystatechange =   Function (){
// When the request has been loaded

If (Req. readystate =   4 ){
// When the request is returned successfully
If (Req. Status =   200 ){ // Or Req. Status <400
// When a successful callback function is defined, the successful callback function is executed.
If (Callback)
Callback (req, data );
}
// When the request returns an error

Else {
// When a failed callback function is defined, the execution of the failed callback function
If (Callbackerror)
Callbackerror (req, data );
}

// Delete XMLHTTP and release resources
Try {
Delete REQ;
REQ =   Null ;
} Catch (E ){}
}
}

// If the post method is used to return to the sending server
If (Method. touppercase () =   " Post " ){
Req. Open ( " Post " , URL, True );
// Whether the request needs to be cached (this item can be set only after Req. open)
If (Cached)
Req. setRequestHeader ( " If-modified-since " , " 0 " );
// The request must be encoded.
If (Urlencoded)
Req. setRequestHeader ( ' Content-Type ' , ' Application/X-WWW-form-urlencoded ' );
Req. Send (data );
Myajaxobj. reqlist. Push (req );
}
// GET request
Else {
Req. Open ( " Get " , URL, True );
// Whether the request needs to be cached
If (Cached)
Req. setRequestHeader ( " If-modified-since " , " 0 " );
Req. Send ( Null );
Myajaxobj. reqlist. Push (req );
}
Return REQ;
}

// Clear all XMLHTTP array elements and release resources
Myajaxobj. clearreqlist =   Function (){
VaR Len = Myajaxobj. reqlist. length;
For ( VaR I =   0 ; I < Len; I ++ ){
VaR REQ = Myajaxobj. reqlist [I];
If (Req ){
Try {
Delete REQ;
} Catch (E ){}
}
}
Myajaxobj. reqlist = [];
}

// Further encapsulate the code when XMLHTTP sends a request in post Mode
// Isclear: whether to clear all elements of the XMLHTTP array. For other parameters, see myajaxobj. Send.
Myajaxobj. sendpost =   Function (URL, Data, callback, isclear, iscached, callbackerror ){
If (Isclear ){
Myajaxobj. clearreqlist ();
}
Myajaxobj. Send (URL, " Post " , Callback, data, True , Iscached, callbackerror ); // The post method must be encoded.
}
// Further encapsulate the code when XMLHTTP sends a request in get Mode
Myajaxobj. sendget =   Function (URL, argS, callback, isclear, iscached, callbackerror ){
If (Isclear)
Myajaxobj. clearreqlist ();
Return Myajaxobj. Send (URL, " Get " , Callback, argS, False , Iscached, callbackerror );
}

call method example:
in my old article "ajax: simple search practice", googlesuggest is achieved, and the code can be transformed into the following call method, the details will not be repeated.

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> /* Call method example (googlesuggest effect):
myajaxobj. sendpost ("ajaxoperations. aspx ", null, googlesuggestcallback, true, true, googlesuggestcallbackerror);
or myajaxobj. sendget ("ajaxoperations. aspx ", null, googlesuggestcallback, false, true, googlesuggestcallbackerror);
*/

Note:
the POST method usually requires encoding and get is not required.
the second to last parameter indicates the cache, which is set to false, you can "solve ie cache problems in Ajax calls".
there is an improved method for creating an XMLHttpRequest pool on the Internet, I have not considered creating multiple XMLHttpRequest instances at a time (not much actually used).
I am familiar with third-party Ajax libraries (such as the Ajax framework set that comes with Microsoft, jquery, etc) readers are recommended to use mature Ajax libraries.
I reiterate that all my technical blogs in the blog Park are prepared based on others' achievements unless the word "original" is marked. Maybe you have read other blogs from the author, so you think you are suspected of gjm. Never doubt your feelings. I have always been ashamed to copy and plagiarize books. From the day I wrote my technology blog, I was waiting for my original ideas to come out. However, the technical capabilities are limited. Forgive me for learning from others' achievements. I will certainly add the source of the original article in the future, where can I create originality when I have time? I don't know how to create originality. It's really hard to create originality.
Finally, click it.

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.