複製代碼 代碼如下:/**//*
非同步請求類
作者:吾非無心
建立時間:2009.2
---------------------------------------------------------------------------------------------------------------------------------
修改記錄:
2009.4.27——添加 URL 檢測功能,如果是“http://xxxx.xxx.xx.xx/..”這樣的格式,使用系統提供的“/getUrl.aspx”進行擷取
以解決跨域問題。
getUrl.aspx 的主要參數有兩個:1、url 值為目標URL(URL中可以帶參數)——必須;2、method 值為代理使用的方法(可選)
如果method為空白,預設採用POST方法進行代理;如果FORM參數為空白(即request.content-length=0),則自動化佈建為GET方法;
(註:getUrl.aspx在2009.4.27重新進行了設計,在原完成代理的基礎上,新增了Cookie代理)
*/
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為非同步
else
this.m_LeesAjaxHttpRequest.open(method, url, true);//true為非同步
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("非同步請求格式錯誤");
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("非同步請求格式錯誤");
return;
}
this.getUrl(vMethod,vUrl,vParams,vSuccess,vFailure,vHeaders);
}
}
/**//*非同步請求類 END*/
使用樣本: 複製代碼 代碼如下:1.//動態裝載JS檔案
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.documentElement.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 .//添加到工具提示豎條上
LeesAjaxRequest.Post({url:"/getToolTip.aspx",
params:{tooltip:title},
headers:{Content_Type:"application/x-www-form-urlencoded;charset=utf-8"},//如果使用Post方法,必須傳入此參數,charset可以為別的
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("產生ToolTip時出錯");
}
});