首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

ajax的两种惯用调用方式

2012-10-25 
ajax的两种常用调用方式方式1:Js代码?//请求js ?script languagejavascript ?var _object ?var xmlh

ajax的两种常用调用方式

方式1:

Js代码?

//请求js ?

<script language="javascript"> ?

var _object; ?

var xmlhttp = null; ?

var interval = null; ?

var rtnData = new Array(); ?

var btype; ?

?

//判断浏览器类型 ?

//ie和firefox对ajax请求回调函数的调用方式不同 ?

function getOs(){ ??

?var OsObject = ""; ??

?if(navigator.userAgent.indexOf("MSIE")>0) { ?

??return "MSIE"; //IE浏览器 ?

?} ??

?if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){ ??

??return "Firefox"; //Firefox浏览器 ?

?} ??

?if(isSafari=navigator.userAgent.indexOf("Safari")>0) { ?

??return "Safari"; //Safan浏览器 ?

?} ??

?if(isCamino=navigator.userAgent.indexOf("Camino")>0){ ??

??return "Camino"; //Camino浏览器 ?

?} ??

?if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){ ??

??return "Gecko"; //Gecko浏览器 ?

?} ?

} ?

?

?

function loadXMLDoc(xmlName,dataTypeId){ ?

?//动态请求调用地址 ?

?var ivkUrl = "<%=path%>/jsp/DataRequest/autoFindRtnDataGTS.jsp?xmlName=" + xmlName + "&"+ Math.random(); ?

?//alert("** ivkUrl = "+ ivkUrl); ?

?

?if (window.XMLHttpRequest){ ?

??xmlhttp=new XMLHttpRequest(); ?

?}else if (window.ActiveXObject){ ?

??xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); ?

?} ?

?

?if (xmlhttp){ ?

??xmlhttp.open("GET",ivkUrl,false); ?

?

?? ? btype = getOs(); ?

?? ? if(btype != "Firefox"){//IE回调state_Change接口 ?

?? ? ?xmlhttp.onreadystatechange = state_Change; ?

??} ? ? ? ?

??xmlhttp.send(null); ?

?

??if(btype == "Firefox"){//Firefox做特殊处理 ?

?? var dataTypeId = "<%=dataTypeId%>"; ? ? ? ?

?? if(dataTypeId == 1){ ?

?? ?updatePage();//页面逻辑 ?

?? } ?

??} ?

?}else{ ?

??alert("Your browser does not support XMLHTTP"); ?

?} ?

?

?//每隔10秒调用1次 ?

?interval = window.setTimeout("loadXMLDoc('<%=xmlName%>','<%=dataTypeId%>')", 10000); ?

?if(rtnData.length == 1 && rtnData[0].downUrl){//1个下载链接 ?

??window.clearTimeout(interval); ?

?} ?

?if(rtnData.length > 1){//多个下载链接 ?

??window.clearTimeout(interval); ?

?} ?

} ?

?

?

function state_Change(){ ?

?var dataTypeId = "<%=dataTypeId%>"; ?

?

?if (xmlhttp.readyState==4){ ?

??if (xmlhttp.status==200){ ?

?? if(dataTypeId == 1){ ?

?? ?updatePage();//页面逻辑 ?

?? } ?

??} ?

?} ?

} ?

?

//js中解析返回数组数据 ?

rtnData = eval(xmlhttp.responseText); ?

?

?

方式2:

?

Js代码?

if (xmlhttp){ ?

?try{ ?

??xmlhttp.open("POST",'DateValidator.do',false); ?

?? ? xmlhttp.send(currStartDate+"##"+currEndDate);//参数传递 ?

?}catch (exception){ ?

??alert(exception); ?

?} ?

?

?//js中解析返回数组数据 ?

?rtnData = xmlhttp.responseText; ?

?.... ? ? ? ?

}else{ ?

?alert("Your browser does not support XMLHTTP"); ?

} ?

?

?

java端处理:

Java代码?

String dateString = (String)requestObj.getData("REQUEST_XMLHTTP");//取得ajax提交的参数 ?

String[] dateArray = dateString.split("##"); ?

?

热点排行