AJAX为什么拿不到值,大家帮我看看错在哪?

qizhan 2006-08-04 08:24:20
result.jsp

--------------------

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<script>

function GetResult(){
if(window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("GET","getresult.jsp",true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send("null");

var strResult = xmlhttp.responseText;
alert(strResult);
}
function MyShow(){
timer = window.setInterval("GetResult()",2000);
}
</script>

<body onload='GetResult()'>
<p></p>
<table width="47%" height="23" border="0" cellpadding="1"
cellspacing="0" id="tb">
<tr>
<td>num1</td>
<td>num2</td>
</tr>
</table>

</body>
</html>


------------------------------------------------------------------------

getresult.jsp
---------------------------------

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.sql.*" import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/good";
String name = "root";
String password = "";

Connection con = DriverManager.getConnection(url, name,
password);
Statement stmt = con.createStatement();
ResultSet rs = stmt
.executeQuery("select * from title");

while (rs.next()) {

response.getWriter().write(rs.getString("content"));

System.out.println(rs.getString("content"));
}

} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
...全文
249 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tomtomclub 2006-08-05
  • 打赏
  • 举报
回复
支持楼上。试试看。。。
qizhan 2006-08-04
  • 打赏
  • 举报
回复
上面的三个方法都 试过了。

alert还是空啊
欢天xi地 2006-08-04
  • 打赏
  • 举报
回复
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
去了.试试.
luoyue2001 2006-08-04
  • 打赏
  • 举报
回复
xmlhttp.open("GET","getresult.jsp",true)->xmlhttp.open("POST","getresult.jsp",true);
改成POST方法试试
chaiwei 2006-08-04
  • 打赏
  • 举报
回复
楼主,你的这句有问题吧

req.send("null");

是不是应该:req.send(null);
chaiwei 2006-08-04
  • 打赏
  • 举报
回复
我也遇到这样的问题

try {
//var xmlDoc = req.responseXML.documentElement;
var xmlDoc = req.responseText;
}
catch(e) {
alert(e);
}
alert(xmlDoc);


req.responseText;这个方法可以取到数据
responseXML.documentElement;这个方法取不到数据
qizhan 2006-08-04
  • 打赏
  • 举报
回复
getresult.jsp肯定 没有问题的

System.out.println(rs.getString("content"));

这一句都打出正常的结果了呀。。。
Arxi 2006-08-04
  • 打赏
  • 举报
回复
你的getresult.jsp能单独运行吗?有可能是你的数据库连接问题
qizhan 2006-08-04
  • 打赏
  • 举报
回复
result.jsp相应部分改为

function GetResult(){
if(window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("GET","getresult.jsp",true);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
alert(xmlhttp.responseText);
}
}
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send("null");

}

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧