【 tulaoshi.com - Web开发 】
代码如下:
/**//*
异步请求类
作者:吾非无心
创建时间: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.length1)
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.length0)
{
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.length1)
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.length1)
{
//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.length1)
{
//alert("异步请求格式错误");
return;
}
this.getUrl(vMethod,vUrl,vParams,vSuccess,vFailure,vHeaders);
}
}
/**//*异步请求类 END*/
使用示例: 代码如下:
1.//动态装载JS文件
if(vJS && vJS.length3)
{
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.length3)
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时出错");
}
});