Ajax如何實現(xiàn)異步提交類、支持跨域

這篇文章給大家分享的是有關(guān)Ajax如何實現(xiàn)異步提交類、支持跨域的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

我們提供的服務(wù)有:網(wǎng)站設(shè)計、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、利辛ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的利辛網(wǎng)站制作公司

 代碼如下:


/**//*
異步請求類

作者:吾非無心
創(chuàng)建時間:2009.2
---------------------------------------------------------------------------------------------------------------------------------

修改記錄:

2009.4.27——添加 URL 檢測功能,如果是“http://xxxx.xxx.xx.xx/..”這樣的格式,使用系統(tǒng)提供的“/getUrl.aspx”進(jìn)行獲取
以解決跨域問題。
getUrl.aspx 的主要參數(shù)有兩個:1、url 值為目標(biāo)URL(URL中可以帶參數(shù))——必須;2、method 值為代理使用的方法(可選)
如果method為空,默認(rèn)采用POST方法進(jìn)行代理;如果FORM參數(shù)為空(即request.content-length=0),則自動設(shè)置為GET方法;
(注:getUrl.aspx在2009.4.27重新進(jìn)行了設(shè)計,在原完成代理的基礎(chǔ)上,新增了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*/


使用示例:

復(fù)制代碼 代碼如下:


1.//動態(tài)裝載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方法,必須傳入此參數(shù),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時出錯");
}
});

感謝各位的閱讀!關(guān)于“Ajax如何實現(xiàn)異步提交類、支持跨域”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

名稱欄目:Ajax如何實現(xiàn)異步提交類、支持跨域
標(biāo)題網(wǎng)址:http://www.muchs.cn/article26/gehecg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站收錄、、微信小程序搜索引擎優(yōu)化、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名