求大神帮忙,关于JS与XMLHttpRequest一个小问题

biwggef 2013-04-27 02:30:34



上面的JS与jsp交互,JS由一个按钮触发,点击按钮后jsp中内容不执行,但是加入被注释掉的alert在运行时会弹出提示框并能使jsp正确执行,求大神帮忙找找问题,不想用那个alert
...全文
123 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
biwggef 2013-04-27
  • 打赏
  • 举报
回复
搞定了,被myeclipse害的,本来就对的,给我一直报错,就把return false给去了,结果纠结了一天,修改了个设置,不再报错了,然后就好了! 非常感谢!
biwggef 2013-04-27
  • 打赏
  • 举报
回复
好了,百度了下,那个错误是myeclipse 的BUG,解决了!
biwggef 2013-04-27
  • 打赏
  • 举报
回复
最开始我加了,可是myeclipse一直在报错,我就去掉了! Cannot return from outside a function or method.
ftiger 2013-04-27
  • 打赏
  • 举报
回复
<form name="register" method="post" onsubmit="fun();return false;"> 要加return false 阻止提交。
biwggef 2013-04-27
  • 打赏
  • 举报
回复
没有alert那行的话,没有反馈信息,加上的话会反馈!
ftiger 2013-04-27
  • 打赏
  • 举报
回复
用这代码测试一点问题也没有,你查别的地方吧。

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
var xhr;
function createXHR() {
	if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (ee) {
				xhr = false;
			}
		}
	} else if (window.XMLHttpRequest) {// 在非IE浏览器中创建XMLHttpRequest对象
		try {
			xhr = new XMLHttpRequest();
		} catch (e) {
			xhr = false;
		}
	}
} 

function reg() {
	var form1 = document.getElementById("form1"),
	d1= form1.userName.value,
	d2= form1.pwd.value,
	d3= form1.email.value;

	createXHR(); 
    xhr.open("POST", "registerin.jsp", true);
	xhr.setRequestHeader("Content-type",
                    "application/x-www-form-urlencoded");
	
	xhr.send("name=" + d1 + "&password=" + d2 + "&email=" + d3);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			var data = xhr.responseText;
			var pNode = document.getElementById("ce");
			pNode.innerHTML = data;
		}
	}; 
}
</script>
</head>

<body>
<form onsubmit="reg(this);return false;" id="form1">
 用户名:<INPUT type="text" name="userName" />
<br /> 密   码:<INPUT type="password" name="pwd" />
<br /> 邮   件:<INPUT type="text" name="email" />
<br /><INPUT type="submit" value="注册" />
</form>
<div id="ce"></div>
</body>
</html>
biwggef 2013-04-27
  • 打赏
  • 举报
回复
这是MyJsp2.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ page language="java"%> <%@ page import="java.sql.*"%> <% out.clear(); request.setCharacterEncoding("UTF-8"); //设置请求体字符编码格式为UTF-8 String a = request.getParameter("a"); if("123".equals(a)){ out.print("<meta http-equiv=\"refresh\" content=\"3;url=index.jsp\" /><div>成功,3秒后跳转<br><a href=\"login.jsp\">直接跳转</a></div>"); } %>
biwggef 2013-04-27
  • 打赏
  • 举报
回复
发个类似的,很简单的.道理相同!问题出现也一样 <%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <style type="text/css"> @import url("all.css");</style> <script> var xhr = null; function fun() { createXHR(); xhr.open("POST", "MyJsp2.jsp", true); var a="123"; xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.send("a="+a); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { var data = xhr.responseText; var pNode = document.getElementById("ce"); pNode.innerHTML = data; } }; alert("1231"); } function createXHR() { if (window.ActiveXObject) // 在IE浏览器中创建XMLHttpRequest对象 { try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (ee) { xhr = false; } } } else if (window.XMLHttpRequest) // 在非IE浏览器中创建XMLHttpRequest对象 { try { xhr = new XMLHttpRequest(); } catch (e) { xhr = false; } } } </script> <body> <div id="ce" class="cen1"> <form name="register" method="post" onsubmit="fun()"> <input type="submit" value="提 交"> </form> </div> </body> </html>
functionsub 2013-04-27
  • 打赏
  • 举报
回复
看起来还是按钮点了以后表单被提交了。。。
biwggef 2013-04-27
  • 打赏
  • 举报
回复
alert后面什么都没有 还有触发事件就是按钮提交表单,上面是提交表单后的操作。 刚又试了下,加alert时都没问题。不加的时候Firefox什么都不执行。换了IE和chrome,JSP中的数据库等处理成功了,但是没有反馈。
ftiger 2013-04-27
  • 打赏
  • 举报
回复
估计不是这一块的问题,alert后面是不是调用了什么动态生成的dom元素?很可能是加载顺序问题导致那个部分出错,影响到这里,那一部分的dom元素在alert之后已经生成了,所以alert后执行成功。
Go 旅城通票 2013-04-27
  • 打赏
  • 举报
回复
一个按钮触发。。什么类型的按钮,不是是submit吧,这样会提交表单,button标签也不行,在w3c浏览器或者css1compat模式下这个也是submit按钮
biwggef 2013-04-27
  • 打赏
  • 举报
回复
function createXHR() { if (window.ActiveXObject) // 在IE浏览器中创建XMLHttpRequest对象 { try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (ee) { xhr = false; } } } else if (window.XMLHttpRequest) // 在非IE浏览器中创建XMLHttpRequest对象 { try { xhr = new XMLHttpRequest(); } catch (e) { xhr = false; } } }
biwggef 2013-04-27
  • 打赏
  • 举报
回复
createXHR(); 
xhr.open("POST", "registerin.jsp", true);
xhr.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xhr.send("name=" + d1 + "&password=" + d2 + "&email=" + d3);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var data = xhr.responseText;
var pNode = document.getElementById("ce");
pNode.innerHTML = data;
}
};
alert("注册成功!");

ftiger 2013-04-27
  • 打赏
  • 举报
回复
代码发出来,谁有空去抄一张图。

52,797

社区成员

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

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