AJAX resume system with js files

Source: Internet
Author: User
Tags call back

Copy codeThe Code is as follows: /*************************************** *******
* @ Author skyz
* @ Function javascript client ajax dealwith
* @ Datetime 2006-3-20
**************************************** ******
* Function: Create a httpRequest object
**************************************** ******/
Function HttpRequest (){
This. _ httpRequest = null; // HttpRequest request object
This. _ callBack = null; // Call back function
This. _ domResult = true; // Result if dom object or text string
This. _ requestData = null; // Request data
This. _ requestResult = null; // HttpRequest result
This. _ stateString = null; // Current request state string
This. _ error = false; // Current if have error
This. _ callBackPara = null; // Current callback function parama
// Internal method for get HttpRequestObject
This. init = function (){
// Judge if Not IE
If (window. XMLHttpRequest ){
This. _ httpRequest = new XMLHttpRequest ();
// Set request mime is text/xml
If (this. _ httpRequest. overrideMimeType ){
This. _ httpRequest. overrideMimeType ('text/xml ');
}
} Else if (window. ActiveXObject ){
Try {
This. _ httpRequest = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (ex ){
Try {
This. _ httpRequest = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (ex ){
This. _ setMessage (ex, true );
Return;
}
}
// This. _ httpRequest. setrequestheader ("Content-Type", "text/xml; charset = gb2312 ");
}
// Judge HttpRequest object create successful
If (! This. _ httpRequest ){
This. _ setMessage ("XMLHttpRequest object creation failed! Please try again... ", true );
Return;
}
}
/*
* Function: Set the request header
* NamePar: request's header name
* ValuePar: request's header value
*/
This. doSetRequestHeader = function (namePar, valuePar ){
If (this. _ error ){
Return;
}
This. _ httpRequest. setRequestHeader (namePar, valuePar );
}
/*
* Function: Set the request data
* DataPar: request's send data;
*/
This. doSetRequestData = function (dataPar ){
If (this. _ error ){
Return;
}
This. _ requestData = dataPar;
}
/*
* Function get RequestHttp Object
*/
This. _ getRequestObj = function (){
If (this. _ error ){
Return;
}
Return this. _ httpRequest;
}
/*
* Function: Set Callback function para
*/
This. doSetCallBack = function (callBack, paraData ){
This. _ callBack = (callBack )? CallBack: null;
This. _ callBackPara = (paraData )? ParaData: null;
};
/*
* Function: Get current stateString
*/
This. doGetState = function (){
Return this. _ stateString;
}
/*
* Function: get current Error
*/
This. doGetError = function (){
Return this. _ error;
}
/*
*
*/
This. doCallBack = function (){
This. _ callBack (this. _ requestResult, this. _ callBackPara );
}

/*
* Function: Send the request
* UrlPar: request's url path
* [MethodPar]: request's method
* [DomPar]: request's result is dom or string
*/
This. doSendResuest = function (urlPar, methodPar, obj, domPar, asyPar ){
If (obj. _ error ){
Return;
}
MethodPar = (methodPar )? MethodPar: "GET ");
AsyPar = (asyPar )? AsyPar: true );
This. _ domResult = (domPar )? DomPar: obj. _ domResult;
Try {
Var a = this. _ getRequestObj ();
A. onreadystatechange = function (){
If (obj. _ error ){
Return;
}
Var readyStateTmp = a. readyState;
If (readyStateTmp = 0 ){
Obj. _ setMessage ("not initialized! ");
} Else if (readyStateTmp = 1 ){
Obj. _ setMessage ("reading ......");
} Else if (readyStateTmp = 2 ){
Obj. _ setMessage ("Read! ");
} Else if (readyStateTmp = 3 ){
Obj. _ setMessage ("switching one by one ......");
} Else if (readyStateTmp = 4 ){
Var statusTmp = a. status;
If (statusTmp = 404 ){
Obj. _ setMessage ("request page not found! ", True );
} Else if (window. location. href. indexOf ("http") ==-1 | statusTmp = 200 ){
Obj. _ setMessage ("finished! ");
If (this. _ domResult & window. XMLHttpRequest ){
Obj. _ requestResult = a. responseXml;
} Else {
Obj. _ requestResult = a. responseText;
}
If (obj. _ callBack ){
Obj. doCallBack ();
}
} Else {
Obj. _ setMessage ("Unknown error! ");
}
} Else {
Obj. _ setMessage ("Unknown error! ");
}
}
A. open (methodPar, urlPar, asyPar );
// A. setRequestHeader ("If-Modified-Since", "0 ");
A. send (obj. _ requestData );
} Catch (ex ){
Obj. _ setMessage (ex, true );
}
}
/*
* Function: Deal exception error
* ExPar: error string
*/
This. _ setMessage = function (exPar, mark ){
This. _ stateString = exPar. toString ();
This. _ error = (mark )? Mark: false;
}
}

Local download

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.