Ajax核心框架函数及例子
2023-06-13 09:14:12 时间
核心ajax(options)函数中,包含了建立xmlhttprequest,提取数据,判断是否回复成功等,基本满足了日常需求。
复制代码代码如下:
//AgenericfunctionforperformmingAJAXrequests
//Ittakesoneargument,whichisanobjectthatcontainsasetofoptions
//Allofwhichareoutlineinthecomments,below
functionajax(options){
//Loadtheoptionsobjectwithdefaults,ifno
//valueswereprovidedbytheuser
options={
//ThetypeofHTTPRequest
type:options.type||"POST",
//TheURLtherequestwillbemadeto
url:options.url||"",
//Howlongtowaitbeforeconsideringtherequesttobeatimeout
timeout:options.timeout||5000,
//Functionstocallwhentherequestfails,succeeds,
//orcompletes(eitherfailorsucceed)
onComplete:options.onComplete||function(){},
onError:options.onError||function(){},
onSuccess:options.onSuccess||function(){},
//Thedatatypethat"llbereturnedfromtheserver
//thedefaultissimplytodeterminewhatdatawasreturnedfromthe
//andactaccordingly.
data:options.data||""
};
//Createtherequestobject
varxml=newXMLHttpRequest();
//OpentheasynchronousPOSTrequest
//xml.open("GET","/some/url.cgi",true);
xml.open("GET",options.url,true);
//We"regoingtowaitforarequestfor5seconds,beforegivingup
vartimeoutLength=5000;
//Keeptrackofwhentherequesthasbeensuccesfullycompleted
varrequestDone=false;
//Initalizeacallbackwhichwillfire5secondsfromnow,cancelling
//therequest(ifithasnotalreadyoccurred).
setTimeout(function(){
requestDone=true;
},timeoutLength);
//Watchforwhenthestateofthedocumentgetsupdated
xml.onreadystatechange=function(){
//Waituntilthedataisfullyloaded,
//andmakesurethattherequesthasn"talreadytimedout
if(xml.readyState==4&&!requestDone){
//Checktoseeiftherequestwassuccessful
if(httpSuccess(xml)){
//Executethesuccesscallbackwiththedatareturnedfromtheserver
options.onSuccess(httpData(xml,options.type));
//Otherwise,anerroroccurred,soexecutetheerrorcallback
}else{
options.onError();
}
//Callthecompletioncallback
options.onComplete();
//Cleanupafterourselves,toavoidmemoryleaks
xml=null;
}
};
//Establishtheconnectiontotheserver
xml.send();
//DeterminethesuccessoftheHTTPresponse
functionhttpSuccess(r){
try{
//Ifnoserverstatusisprovided,andwe"reactually
//requestingalocalfile,thenitwassuccessful
return!r.status&&location.protocol=="file:"||
//Anystatusinthe200rangeisgood
(r.status>=200&&r.status<300)||
//Successfulifthedocumenthasnotbeenmodified
r.status==304||
//Safarireturnsanemptystatusifthefilehasnotbeenmodified
navigator.userAgent.indexOf("Safari")>=0&&typeofr.status=="undefined";
}catch(e){}
//Ifcheckingthestatusfailed,thenassumethattherequestfailedtoo
returnfalse;
}
//ExtractthecorrectdatafromtheHTTPresponse
functionhttpData(r,type){
//Getthecontent-typeheader
varct=r.getResponseHeader("content-type");
//Ifnodefaulttypewasprovided,determineifsome
//formofXMLwasreturnedfromtheserver
vardata=!type&&ct&&ct.indexOf("xml")>=0;
//GettheXMLDocumentobjectifXMLwasreturnedfrom
//theserver,otherwisereturnthetextcontentsreturnedbytheserver
data=type=="xml"||data?r.responseXML:r.responseText;
//Ifthespecifiedtypeis"script",executethereturnedtext
//responseasifitwasJavaScript
if(type=="script")
eval.call(window,data);
//Returntheresponsedata(eitheranXMLDocumentoratextstring)
returndata;
}
}
在同等目录中,我们可以建立一个rss.xml文件,用这个函数来访问。
rss.xml如下:
<titles>
<title>
缘份
</title>
<title>
月亮
</title>
<title>
缘份月亮
</title>
</titles>
再建立一个html文档,调用它,就能看到rss.xml中的内容就能被访问到。
整体看看,其实真的比较简洁和简单。不仅是能访问xml格式文件,html,.js格式的文件都可以调用的;
这些都可以在本地建立对应的文件,进行调用,都可以实现。
相关文章
- Ajax-hook拦截全局ajax请求操作
- ajax发送异步请求四个步骤,AJAX的异步请求的四个步骤[通俗易懂]
- 什么是Ajax以及ajax请求的步骤[通俗易懂]
- Ajax教程_ajax是服务器端动态网页技术
- .ajax get 写法,原生Ajax写法(GET)
- Ajax 如何解决跨域问题
- WPJAM Basic 实现文章后台列表页全 AJAX 操作
- AJAX技术之DWR框架详解编程语言
- ashx文件获取$.ajax()方法发送的数据详解编程语言
- Ajax技术实现MySQL数据查询与更新(ajax连接mysql)
- Ajax调用MySQL给网站带来快捷体验(ajax调用mysql)
- 使用Ajax实现MySQL数据库操作(ajax操作mysql)
- 使用Ajax和Mysql实现简单点赞功能(ajax mysql点赞)
- AJAX技术简化Oracle数据库的访问处理(ajax连接oracle)
- Ajax实现对Oracle的异步操作(ajax操作oracle)
- 异步Ajax与Oracle数据库实现无缝连接(ajax连接oracle)
- 国内首发--ajax完整功能框架
- javascript拖动_cookie_ajax等
- AJAX简介及入门实例
- PHP开源AJAX框架14种
- jquery框架使用教程AJAX篇
- AJax学习笔记二(onreadystatechange的作用)
- ajax不执行success回调而是执行了error回调
- ajax页面无刷新IE下遭遇Ajax缓存导致数据不更新的问题
- PHP开发框架kohana中处理ajax请求的例子
- Ajax中浏览器和服务器交互详解