JSP页面中的button按钮的onclick事件不能触发

冷俊不惊 2011-08-18 04:58:26
JSP页面中的button按钮的onclick事件不能触发,从新发布也没用,两个button按钮都没有用。
求指点。

<script type="text/javascript">

var xmlHttp;

function createXMLHttpRequest(){
try{
xmlHttp=new XMLHttpReques();
}catch(e){
try{
xmlHttp=new ActiveXObject(Msxm12.XMLHTTP);
}catch(e){
try{
xmlHttp=new ActiveXObject(Microsoft.XMLHTTP);
}catch{
alert("你的浏览器版本太老了!");
}
}
}
}

function handlestatechange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
alert("文件内容:"+xmlHttp.responseText);
}
}
}

function startRequest(){
createXMLHttpRequest();
xmlHttp.onreadystatechange=handlestatechange;
xmlHttp.open("post","web.xml",true);
xmlHttp.send(null);
}

function start(){
alert("11111111111");
}

</script>

<body>
This is my JSP page. <br>
<input type="button" id="bt1" value="提交" onclick="startRequest();">
<input type="button" id="bt2" value="测试" onclick="start();">
</body>
...全文
4243 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunnylonglong 2011-08-30
  • 打赏
  • 举报
回复
肯定可以调用的,肯定是那其他那个部分写错了,导致了不能执行!
aliujuani 2011-08-30
  • 打赏
  • 举报
回复
xmlHttp=new XMLHttpReques();
这都写错了
冷俊不惊 2011-08-22
  • 打赏
  • 举报
回复
那位高人指点下,为什么 readyState和status的值都是0.还有 xmlHttp.ResponseText的值是空的,谢谢!
冷俊不惊 2011-08-21
  • 打赏
  • 举报
回复
<%@ page language="java" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</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">
-->
</head>
<script type="text/javascript">

var xmlHttp;
function createXMLHttpRequest(){
try{
xmlHttp=new XMLHttpRequest();
}catch(e){
try{
xmlHttp=ActiveXObject("Msxm12.XMLHTTP");
}catch(e){
try{
xmlHttp=ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
alert("浏览器版本过旧!");
}
}
}

}

function startTest(){
createXMLHttpRequest();
if(xmlHttp!=null){
xmlHttp.onreadystatechange=test();
xmlHttp.open("post","F:\workspace\java\ajax\WebRoot.txt",true);
xmlHttp.send(null);
}
}

function test(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
alert("test文档内容:"+xmlHttp.responseTest);
}else{
alert("xmlHttp.status:"+xmlHttp.status);
}
}else{
alert("xmlHttp.readyState:"+xmlHttp.readyState);
alert("xmlHttp.status:"+xmlHttp.status);
[color=#FFFF00] alert("txt文件内容:"+xmlHttp.responseText);
[/color]
}
}
</script>

<body>
<input type="submit" id="bt_1" value="创建XMLHttpRequest对象" onclick="startTest();">
</body>
</html>

代码经过修改还有一点问题。求指点!
1.代码执行了红色字体部分,使用 firedebug发现 readyState=0,status=0;为什么他们的数值是0?
2.另外黄色字体部分 执行结果中xmlHttp.responseText没有结果,是空的,但txt文件中确实有字符啊!

求指点?谢谢
坐观垂钓者 2011-08-19
  • 打赏
  • 举报
回复
好好检查你的JS
keaixiaozhu7688 2011-08-19
  • 打赏
  • 举报
回复
IE8以上版本的话。直接F12不解释
wwilovehome 2011-08-19
  • 打赏
  • 举报
回复
不能触发很简单,JS代码出错,要不然不存在不能触发的现象,建议使用alert()逐步调试。
beiouwolf 2011-08-19
  • 打赏
  • 举报
回复
js差错的好工具:Firefox
简单一点的直接用错误控制台
工具 - 错误控制台 5.0以上在快捷菜单 - web开发人员 - 错误控制台

更进一步的话,安装firebug插件,可以跟踪js执行情况
24K純帥 2011-08-19
  • 打赏
  • 举报
回复
if(xmlHttp.status==200){
startRequest();
}
冷俊不惊 2011-08-19
  • 打赏
  • 举报
回复
出现浏览器版本太来是因为javascript中代码的错误,改正后,有有新问题了:

<script type="text/javascript">

var xmlHttp;
function createXMLHttpRequest(){
try{
xmlHttp=new XMLHttpRequest();
}catch(e){
try{
xmlHttp=ActiveXObject("Msxm12.XMLHTTP");
}catch(e){
try{
xmlHttp=ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
alert("浏览器版本过旧!");
}
}
}

}

function startTest(){
createXMLHttpRequest();
xmlHttp.onreadystatechange=test();
xmlHttp.open("post","test.txt",true);
xmlHttp.send(null);
}

function test(){
if(xmlHttp.readyState==4){
alert("test文档内容:"+xmlHttp.responseTest);
}
}
</script>

<body>
<input type="submit" id="bt_1" value="创建XMLHttpRequest对象" onclick="startTest();">
</body>

onclick事件可以发生,但是不能得到相应的弹出窗口和信息,使用firedebug 得到的结果 readyState=0 ,
把代码中改为 xmlHttp.readyState==0 时 可以得到弹出窗口 ,但是提示 “test文档内容:undefined”

本人新手求解答。
GIS__ 2011-08-19
  • 打赏
  • 举报
回复

<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}

function state_Change()
{
if (xmlhttp.readyState==4)
{
if (xmlhttp.status==200)
{
alert("文件内容:"+xmlhttp.status);
}
else
{
alert("Problem retrieving XML data:" + xmlhttp.responseText);
}
}
}
function startz(){
alert("11111111111");
}
</script>
</head>

<body>
<h2>Using the HttpRequest Object</h2>


<button onclick="startz()">TEST</button>
<button onclick="loadXMLDoc('c:/web.xml')">TEST_XML</button>



完整的ajax代码
luohuijun619 2011-08-19
  • 打赏
  • 举报
回复
用FireFox,有fireDebug
果-果 2011-08-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 yanrutong 的回复:]
还有个问题 :用的 goggle chrome 浏览器 14.0.803.0 居然显示浏览器太老! 这浏览器真有那么老?
[/Quote]
一般都和ie9和firefox
GIS__ 2011-08-19
  • 打赏
  • 举报
回复

<html>
<head>
<title>test</title>
<script type="text/javascript">
function test(){
alert("test");
}
function startS(){
alert("11111111111");
}

</script>
</head>
<body>


<body>
<input type="button" id="bt1" value="提交" onclick="startRequest();">
<input type="button" id="bt2" value="测试1" onclick="startS()">
<input type="button" id="bt3" value="测试2" onclick="test()">
</body>
</html>

start()这个方法是js内部的,你不能随便叫这个名字啊
rainbowful 2011-08-18
  • 打赏
  • 举报
回复
JS很麻烦,很容易出现错误,学习中...
冷俊不惊 2011-08-18
  • 打赏
  • 举报
回复
还有个问题 :用的 goggle chrome 浏览器 14.0.803.0 居然显示浏览器太老! 这浏览器真有那么老?
冷俊不惊 2011-08-18
  • 打赏
  • 举报
回复
同样谢谢其他各位,能否给点建议:比如说写JS 的一些注意事项、和建议以及如何发现JS其中的错误。谢谢!
冷俊不惊 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 shuaizai88 的回复:]

try{
xmlHttp=new ActiveXObject(Microsoft.XMLHTTP);
}catch{
alert("你的浏览器版本太老了!");
}
catch少了(e)

还有你js中还有很多错误、、
给分吧、哈哈
[/Quote]

就 catch 后面少了 e ,加上后就正常了,还有什么地方错。说来看看,说不全部给全分,你太打击我了!11
  • 打赏
  • 举报
回复
try{
xmlHttp=new ActiveXObject(Microsoft.XMLHTTP);
}catch{
alert("你的浏览器版本太老了!");
}
catch少了(e)

还有你js中还有很多错误、、
给分吧、哈哈
sxb372435741 2011-08-18
  • 打赏
  • 举报
回复
js错了。用火狐调试js看看什么地方写错了。
加载更多回复(3)

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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