varrep;
functionshowDatas() { if(window.XMLHttpRequest){//firefox,safari req=newXMLHttpRequest(); }elseif(window.ActiveXObject){//IE try{ req=newActiveXObject("Microsoft.XMLHTTP"); }catch(e){ req=newActiveXObject("Msxml2.XMLHTTP"); } } req.onreadystatechange=callExec;//指定回调函数callExec req.open("post","<%=basePath%>indexAction!findDatas.action");//创建一个get请求 req.send(null);//发送请求 } //请求响应回来后,将响应信息显示到页面上 functioncallExec(){ if(req.readyState==4){ //alert(req.responseText); $("#divId").html(req.responseText);}
}
其中后台的findDatas方法返回一个重组的div串(代码大体如下):
StringBufferbuffer=newStringBuffer();
buffer.append("<divclass='goodNum_body'>");
buffer.append(...); buffer.append("</div>");
response.setContentType("text/html;charset=utf-8"); out=response.getWriter(); out.print(buffer.toString()); out.flush();
----------------------------------以下为原生态ajax运用模版-----------------------------------------
<scripttype="text/javascript"> varxmlhttp; functionloadXMLDoc(url) { xmlhttp=null; if(window.XMLHttpRequest) {//codeforallnewbrowsers xmlhttp=newXMLHttpRequest(); } elseif(window.ActiveXObject) {//codeforIE5andIE6 xmlhttp=newActiveXObject("Microsoft.XMLHTTP"); } if(xmlhttp!=null) { xmlhttp.onreadystatechange=state_Change; xmlhttp.open("GET",url,true); xmlhttp.send(null); } else { alert("YourbrowserdoesnotsupportXMLHTTP."); } } functionstate_Change() { if(xmlhttp.readyState==4) {//4="loaded" if(xmlhttp.status==200) {//200=OK //...ourcodehere... } else { alert("ProblemretrievingXMLdata"); } } } </script> (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|