问题是这样的,我在一个页面用onload的函数掉用AJAX脚本去数据库中取一些记录回来,在用document.write()打印到本页,可这样,这页面运行正常,但运行15次的时候就会停止响应,页面什么都取不到了,这是为什么呢?下面是我的代码?大家请看:<%@ page language="java" pageEncoding="GB2312"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>showstuff.jsp</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--><script>var req;function createXMLHttpRequest(){ if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } }function getResult(state) {
var url = "showstuff?state="+state+"&data="+new Date().getTime(); //data数据为了处理输入框有时输入相同信息Ajax不跳转问题,后台并未对此数据操作
createXMLHttpRequest();
if(req){
req.open("GET",url, true); req.onreadystatechange = complete; //调用complete()方法
req.send(null); }
} function complete(){ if (req.readyState == 4) {
alert("4");
if (req.status == 200) { var a=req.responseText;
document.write("<META http-equiv='Content-Type' content='text/html; charset=GB2312' /> <style type='text/css'> p{font-family:宋体; font-size:13px;} li{font-family:宋体; font-size:13px;}</style> <body background='../images/bj31.jpg' style='background-repeat: repeat-x; background-attachment:fixed;' >"+a);
}
} }</script> </head> <body onload="getResult(<%=request.getParameter("id") %>)"> //取上一页传过来的参数ID来判断取那条记录
}
<br>
</body>
</html:html>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>showstuff.jsp</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--><script>var req;function createXMLHttpRequest(){ if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } }function getResult(state) {
var url = "showstuff?state="+state+"&data="+new Date().getTime(); //data数据为了处理输入框有时输入相同信息Ajax不跳转问题,后台并未对此数据操作
createXMLHttpRequest();
if(req){
req.open("GET",url, true); req.onreadystatechange = complete; //调用complete()方法
req.send(null); }
} function complete(){ if (req.readyState == 4) {
alert("4");
if (req.status == 200) { var a=req.responseText;
document.write("<META http-equiv='Content-Type' content='text/html; charset=GB2312' /> <style type='text/css'> p{font-family:宋体; font-size:13px;} li{font-family:宋体; font-size:13px;}</style> <body background='../images/bj31.jpg' style='background-repeat: repeat-x; background-attachment:fixed;' >"+a);
}
} }</script> </head> <body onload="getResult(<%=request.getParameter("id") %>)"> //取上一页传过来的参数ID来判断取那条记录
}
<br>
</body>
</html:html>
String english = request.getParameter("english");
if(english!=null && !english.equals("")){
long id = 0;
if (!state .equals("null")) {
id = Long.parseLong(state); Matter1DAO dao = (Matter1DAO) WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext())
.getBean("Matter1DAO");
List list = dao.findByProperty("cid", id);
Matter1 matter1 = null;
if (list.size() != 0 && list != null) {
matter1 = (Matter1) list.get(0);
String str = matter1.getSContent();
PrintWriter out = response.getWriter(); out.write(str); out.close();
}
}
很简单啊,不能出问题呀
没发现你的定时器
就是说你只有在打开页面的时候调用了一次该方法,然后请求处理页面再返回结果
也许是超时就没response了在onload的函数里写一个定时器 调用就可以了