JAVA WEB 新手求助!$.post();的回调函数不执行,怎么从Servlet中获取到返回的数据

Sky_小小苏 2016-06-02 03:48:51
最近写一个用户登陆的界面
这是核心HTML代码

<form>
<!-- <form action="login.jsp" method="post" onsubmit="return validate_form(this)"> -->
<div id=div1>
<table id=login height="100%" cellspacing=0 cellpadding=0 width=800
align=center>
<tbody>
<tr id=main>
<td>
<table height="100%" cellspacing=0 cellpadding=0 width="100%">
<tbody>
<tr>
<td colspan=4> </td>
</tr>
<tr height=30>
<td width=380> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr height=40>
<td rowspan=4> </td>
<td>用户名:</td>
<td><input class=textbox id="txtusername" name="username"></td>
<td width=120 id="usernameTd"> </td>
</tr>
<tr height=40>
<td>密 码:</td>
<td><input class=textbox id="txtuserpassword" type=password name="userpsw"></td>
<td width=120 id="passwordTd"> </td>
</tr>

<tr height=40>
<td></td>
<td align=left>    
<input id="btnlogin" type="button" value=" 登 录 ">   
<input id="btnreset" type="reset" value=" 重 填 " name="btnreset">
</td>
<td id="result" width=120> </td>
</tr>
<tr height=110>
<td colspan=4> </td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr id=root height=104>
<td> </td>
</tr>
</tbody>
</table>
</div>
<div align="center">    </div>
<div id=div2 style="display: none"></div>
</form>

这是js代码

$(document).ready(function(){
// 检测用户是否输入用户名
$("#txtusername").blur(function() {
if ($("#txtusername").val() == "") {
$("#usernameTd").html("<span style=\"color:red;\">请输入用户名</span>");
}else{
$("#usernameTd").hide();
}
});
// 检测用户是否输入密码
$("#txtuserpassword").blur(function() {
if ($("#txtuserpassword").val() == "") {
$("#passwordTd").html("<span style=\"color:red;\">请输入密码</span>");
}else{
$("#passwordTd").hide();
}
});
// 提交验证
$("#btnlogin").click(function(){
var userName = $("#txtusername").val();
var userPassword = $("#txtuserpassword").val();
if(userName == ""){
alert("用户名不能为空");
}else if(userPassword == ""){
alert("密码不能为空");
}
else{
$.post("http://127.0.0.1:8080/QMSystem/MyServlet",
{
username:userName,
userpsw:userPassword
},
function(data,status){
$("#result").html("<span style=\"color:red;\">用户名或密码错误</span>");
alert("用户名不存在或密码错误" + data);
}
);
}
});
});

这是servlet部分代码

//2.操作数据库:导入数据库驱动
//注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/QMSystem","root","root");
//拼写SQL语句
String sql ="select * from user";
//准备SQL语句
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
Boolean lable = false;
PrintWriter out = response.getWriter();
while(rs.next()){
this.log("sql is execute."+ rs.getString("name") + " " + rs.getString("password"));
if(userName.equals(rs.getString("name")) && userPassword.equals(rs.getString("password"))){
String userPower = rs.getString("power");
lable = true;
// session.setAttribute("name", userN ame);//保存当前登录的用户名
this.log("conn success is execute.");
// String result = stationService.queryStationByName(name);

response.sendRedirect("http://127.0.0.1:8080/QMSystem/mainIndex.html");
out.println("true");
out.flush();
out.close();
break;
}
}
if(lable == false){
this.log("conn false is execute.");
// response.setContentType("text/x-json;charset=UTF-8");
String result = "false";
out.println(result);
out.flush();
out.close();
}
//关闭数据库连接
conn.close();


现在我想实现的是登陆的时候,把用户输入的表单和数据库存的数据比较,正确就跳转到下一个页面,错误给出提示。
上面的代码js的$.post();的回调函数怎么没有执行,求助!我的servlet代码写的有问题吗,log信息已经能在控制台打出来了

...全文
248 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sky_小小苏 2016-06-03
  • 打赏
  • 举报
回复
跳转我找到了,但是data == "false"这个问题还是没搞好,求助!

function(data,status){
					if(data == "false") // response响应我设置的text/string,为什么到这里匹配不上,进不去if
					{	
						$("#result").html("<span style=\"color:red;\">用户名或密码错误</span>");
					}else{
						// 跳转页面
						self.location='/QMSystem/mainIndex.html';
					}					
				}
Sky_小小苏 2016-06-03
  • 打赏
  • 举报
回复
引用 2 楼 xiaofanku 的回复:
2、验证成功,怎么跳转页面?用response.sendRedirect()试了下怎么没反应? re: 你要在客户端跳转?还是服务器端跳转 1、回调函数为什么不执行,数据类型不对吗?要怎么写。 re: 打开浏览器的开发者工具,切换到网络中的xhr 开发的时候用chome或firefox,不要用ie,ie的开发者工具已无法描述
谢谢,用firefox看了下,url写的有问题。现在能进入回调函数了。 还有个些问题想请教,我要客户端跳转怎么实现,还有response返回的数据怎么匹配不上

$.post("/QMSystem/MyServlet",
				{
					username:userName,
					userpsw:userPassword
				},	
				function(data,status){
					if(data == "false") // response响应我设置的text/string,为什么到这里匹配不上,进不去if
					{	
						$("#result").html("<span style=\"color:red;\">用户名或密码错误</span>");
					}else{
						// 这里跳转页面怎么写
					}					
				}
			);
Sky_小小苏 2016-06-03
  • 打赏
  • 举报
回复
找到原因了,out.println()这个带有换行,改成out.print()就OK.
街头小贩 2016-06-02
  • 打赏
  • 举报
回复
2、验证成功,怎么跳转页面?用response.sendRedirect()试了下怎么没反应?
re:
你要在客户端跳转?还是服务器端跳转

1、回调函数为什么不执行,数据类型不对吗?要怎么写。
re:
打开浏览器的开发者工具,切换到网络中的xhr

看一看你的请求返回什么了


查看请求的响应


开发的时候用chome或firefox,不要用ie,ie的开发者工具已无法描述
Sky_小小苏 2016-06-02
  • 打赏
  • 举报
回复
主要有两个问题,1、回调函数为什么不执行,数据类型不对吗?要怎么写。2、验证成功,怎么跳转页面?用response.sendRedirect()试了下怎么没反应?

52,778

社区成员

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

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