Ajax asynchronous (request) Submission class supports cross-Origin

Source: Internet
Author: User
Tags ojs

Copy codeThe Code is as follows :/**//*
Asynchronous request class

Author: I do not care
Creation Time: 2009.2
Else ---------------------------------------------------------------------------------------------------------------------------------

Modification record:

2009.4.27 -- add the URL detection function. If the format is "http://xxxx.xxx.xx.xx/..htm", use the example /geturl.aspx.pdf of the system to get it.
To solve cross-origin problems.
The main parameters of getUrl. aspx include: 1. the url value is the target URL (the URL can contain parameters) -- required; 2. the method value is the method used by the proxy (optional)
If the method is blank, the POST method is used as the proxy by default. If the FORM parameter is blank (request. content-length = 0), the GET method is automatically set;
(Note: getUrl. aspx is re-designed in 2009.4.27, and Cookie proxy is added on the basis of the original proxy)
*/
Function IsOuterURL (str_url ){
Var strRegex = "(https | http): //) ([\ w-] + \\.) + [\ w-] +. ([^ a-z]) (/[\ w -. /? % & =] *)? | (Https | http): // [a-zA-Z0-9 \-\.] [\ w-] +. ([^ a-z]) (/[\ w -. /? % & =] *)? ";
Var re = new RegExp (strRegex );

If (re. test (str_url )){
Return (true );
} Else {
Return (false );
}
}
Var LeesAjaxRequest =
{
List: [],
M_LeesAjaxHttpRequest: null,
IsWorking: false,
GetUrl: function (method, url, params, successFun, FailureFun, headers)
{
If (url. length <1)
Return;
If (this. m_LeesAjaxHttpRequest = null)
{
If (typeof XMLHttpRequest! = 'Undefined ')
{
This. m_LeesAjaxHttpRequest = new XMLHttpRequest ();
}
Else if (typeof ActiveXObject! = 'Undefined ')
{
This. m_LeesAjaxHttpRequest = new ActiveXObject ('Microsoft. xmlhttp ');
}

}
If (this. m_LeesAjaxHttpRequest)
{
If (this. isWorking)
{
This. list [this. list. length] = {method: method, url: url, params: params, successFun: successFun, failureFun: FailureFun, headers: headers };
}
Else
{
This. isWorking = true;
If (IsOuterURL (url ))
This. m_LeesAjaxHttpRequest.open (method, "/geturl. aspx? Url = "+ escape (url) +" & method = "+ method, true); // true is asynchronous
Else
This. m_LeesAjaxHttpRequest.open (method, url, true); // true is asynchronous
Var _ this = this;
This. m_LeesAjaxHttpRequest.onreadystatechange = function ()
{
If (4 = _ this. m_LeesAjaxHttpRequest.readyState)
{
If (200 = _ this. m_LeesAjaxHttpRequest.status)
{
If (successFun)
{
Try {
SuccessFun (_ this. m_LeesAjaxHttpRequest );
}
Catch (ex)
{}
}
}
Else
{
If (FailureFun)
{
Try {
FailureFun (_ this. m_LeesAjaxHttpRequest );
}
Catch (ex)
{}
}
}
_ This. isWorking = false;
If (_ this. list. length> 0)
{
Var o = _ this. list [0];
_ This. list. splice (0, 1 );
_ This. getUrl (o. method, o. url, o. params, o. successFun, o. failureFun, o. headers );
}
}
};
Var vPara = "";
If (typeof params = "string ")
{
VPara = escape (params );
}
Else if (params)
{
Try
{
For (var e in params)
{
If (vPara. length <1)
VPara = e + "=" + escape (params [e]);
Else
VPara + = "&" + e + "=" + escape (params [e]);
}
}
Catch (ex)
{}
}
If (headers)
{
Try
{
For (var h in headers)
{
This. m_LeesAjaxHttpRequest.setRequestHeader (h. replace ("_", "-"), headers [h]);
}
}
Catch (ex)
{
}
}
This. m_LeesAjaxHttpRequest.send (vPara );
}
}
},
Post: function (o)
{
Var vMethod = o. method | "post ";
Var vUrl = o. url | null;
Var vParams = o. params | "";
Var vSuccess = o. success | null;
Var vFailure = o. failure | null;
Var vHeaders = o. headers | null;
If (vUrl = null | vUrl. length <1)
{
// Alert ("asynchronous request format error ");
Return;
}
This. getUrl (vMethod, vUrl, vParams, vSuccess, vFailure, vHeaders );
},
Get: function (o)
{
Var vMethod = o. method | "get ";
Var vUrl = o. url | null;
Var vParams = o. params | "";
Var vSuccess = o. success | null;
Var vFailure = o. failure | null;
Var vHeaders = o. headers | null;
If (vUrl = null | vUrl. length <1)
{
// Alert ("asynchronous request format error ");
Return;
}
This. getUrl (vMethod, vUrl, vParams, vSuccess, vFailure, vHeaders );
}
}
/** // * Asynchronous request class END */

Example: Copy codeCode: 1. // dynamically load JS files
If (vJS & vJS. length> 3)
{
If (! _ This. loadedJS. isInArray (vJS ))
{
_ This. loadedJS [_ this. loadedJS. length] = vJS;
LeesAjaxRequest. Get ({
Url: vJS,
Success: function (ojs ){
Var jsOBJ = document. createElement ("script ");
JsOBJ. text = ojs. responseText;
Document.doc umentElement. appendChild (jsOBJ );
}, // End success
Headers: {Content_Type: "application/x-javascript "}
}); // End Get
} // End if (! _ This. loadedJS. isInArray (vJS ))
} // End if (vJS & vJS. length> 3)

2. // Add it to the toolbar with the prompt vertical bar
LeesAjaxRequest. Post ({url: "/getToolTip. aspx ",
Params: {tooltip: title },
Headers: {Content_Type: "application/x-www-form-urlencoded; charset = UTF-8"}, // This parameter must be input if the Post method is used. charset can be another
Success: function (o ){
Var rObj = eval ("(" + o. responseText + ")");
Var tObj = new LeesBaseWindow (rObj ["width"] + 2, rObj ["height"] + 1 ,"","","","","", 1 ,"");
TObj. ShowWindow (vToolBarSlider. window );
TObj. contentWindow. style. backgroundImage = "url (" + rObj ["image"] + ")";
TObj. contentWindow. style. marginLeft = "1px ";
TObj. contentWindow. style. backgroundRepeat = "no-repeat ";
TObj. contentWindow. style. backgroundPosition = "0 0 ";
TObj. contentWindow. onmouseover = function (){
This. style. backgroundPosition = "0-" + rObj. height;
_ This. setShowWindow (obj );
}
TObj. contentWindow. onmouseout = function (){
This. style. backgroundPosition = "0 0 ";
}
},
Failure: function (){
Alert ("An error occurred while generating ToolTip ");
}
});

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.