jspajax自动刷新:jsp+ajax自动刷新例子(转)来源: 发布时间:星期一, 2009年11月30日 浏览:882次 评论:0
、处理ajax请求jsp文件:auto.jsp
<%@ page contentType="text/html; char=gb2312" %> <% //设置输出信息格式及集 response.ContentType("text/xml; char=UTF-8"); response.Header("Cache-Control","no-cache"); out.prln("<response>"); for( i=0;i<2;i){ out.prln("<name>"+()(Math.random*10)+ "型笔记本</name>"); out.prln("<count>" +()(Math.random*100)+ "</count>"); } out.prln("</response>"); out.close; %> 2、发送ajax请求html文件:autoRefresh.html <head> <META http-equiv=Content-Type content="text/html; char=gb2312"> </head> <script language="javascript"> var XMLHttpReq; //创建XMLHttpRequest对象 function createXMLHttpRequest { (window.XMLHttpRequest) { //Mozilla 浏览器 XMLHttpReq = XMLHttpRequest; } (window.ActiveXObject) { // IE浏览器 try { XMLHttpReq = ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { XMLHttpReq = ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } } //发送请求 function sendRequest { createXMLHttpRequest; var url = "auto.jsp"; XMLHttpReq.open("GET", url, true); XMLHttpReq.onreadystatechange = processResponse;//指定响应 XMLHttpReq.send(null); // 发送请求 } // 处理返回信息 function processResponse { (XMLHttpReq.readyState 4) { // 判断对象状态 (XMLHttpReq.status 200) { // 信息已经成功返回开始处理信息 DisplayHot; Timeout("sendRequest", 1000); } { //页面不正常 window.alert("您所请求页面有异常"); } } } function DisplayHot { var name = XMLHttpReq.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue; var count = XMLHttpReq.responseXML.getElementsByTagName("count")[0].firstChild.nodeValue; document.getElementById("product").innerHTML = name; document.getElementById("count").innerHTML = count; } </script> <body _disibledevent=> <table style="BORDER-COLLAPSE: collapse" borderColor=#111111 cellSpacing=0 cellPadding=0 width=200 bgColor=#f5efe7 border=0> <TR> <TD align=middle bgColor=#dbc2b0 height=19 colspan="2"><B>正在热卖笔记本</B> </TD> </TR> <tr> <td height="20"> 型号:</td> <td height="20" id="product"> </td> </tr> <tr> <td height="20"> 销售数量:</td> <td height="20" id="count"> </td> </tr> </body> </table> 0
相关文章读者评论
发表评论 |