一个空行,执行结果截然不同?(jsp+jquery简单实例)

gothing 2009-04-09 08:27:55
附件上传不了,只有粘贴了:jsp+jquery+ajax的一个简单的实例,其中login.jsp首行为空行时,结果就错误了,实在不可思议?? 三个文件:login.htm、login.js、login.asp
login.htm:
<html>
<head>

<script src="js/jquery.js" type="text/javascript"> </script>
<script src="js/login.js" type="text/javascript"> </script>
<script src="lib/jquery.tablesorter.js" type="text/javascript"> </script>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<style type="text/css">
body { font:12px/1.6em "宋体" }
ul { list-style:none; }
li { margin-top:10px; }
</style>
</head>

<body>
<h3>会员登录 </h3>
<div id="login">
<form method="post" action="login.asp" id="form2">
<div id="confirm">  </div>
<ul>
<a href = "#">aa </a>
<li>名字: <input type="text" id="user" size="16" maxlength="20"> </li>
<li>密码: <input type="password" id="pass" size="16" maxlength="20"> </li>
<li>
<input type="submit" style="border:0" />
</li>
</ul>
</form>
</div>
</body> </html>

login.js:
$(document).ready
(
function()
{
$("#form2").submit
(
function()
{
login();
return false;
}
);
}
);

function login()
{
var user = $("#user").val();
var pass = $("#pass").val();
if (user == "")
{
$("#confirm").html("请输入登录用户名");
$("#user").focus();
return false;
}
if(pass == "")
{
$("#confirm").html("请输入登录密码");
$("#pass").focus();
return false;
}

$.ajax({
type: "POST",
url: "login.jsp",
data: "user=" + user + "&psw=" + pass,
beforeSend: function(){
$("#confirm").html("登录中,请稍候......");
},

success: function(msg){
alert(msg);
if(msg == "success"){
$("#confirm").html("登录成功,欢迎" + user + "回来!正在进入你的空间......");

// window.location.href("success.htm");
// document.location.href = "success.htm";
}else {
$("#confirm").html("没有此用户或者密码不正确!");
}
// $("#confirm").html(msg);
}
});
}

login.jsp:
<%

String id = (String)request.getParameter("user");
String p = (String)request.getParameter("psw");

//out.print(id+' ' +p);

if( (id!=null && id.equals("admin")) && (p!=null && p.equals("admin")) )
{
out.print("success");
}
else
{
out.print("failed");
}

%>
...全文
458 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
挨踢男 2011-05-21
  • 打赏
  • 举报
回复
学习 中 ,,,,
caonimahaoma777 2011-05-19
  • 打赏
  • 举报
回复


...


现在很多都封装的函数,

进进出出很困难阿。

zcfavoritegy 2011-05-18
  • 打赏
  • 举报
回复
msg 是什么意思呢?
阿非 2009-04-17
  • 打赏
  • 举报
回复
out.clear();
out.print("success");
-----------
最好后面再加上类似 out.end();这样的函数
gothing 2009-04-09
  • 打赏
  • 举报
回复
搞定,在输出返回值之前先清除所有其他输出:
out.clear();
out.print("success");

out.clear();
out.print("failed");

看似简单,处处机关,体现本人对jsp等的基础不牢
gothing 2009-04-09
  • 打赏
  • 举报
回复
知道为何会结果不同了,没有空行时login.jsp输出的只有"success",有空行或其他字符时login.jsp输出所有其他字符和success,而login.js的比较“if(msg == "success"){
”肯定就不匹配了。

现在的问题是肯定要在login.jsp中写其他代码,如何让login.js中的ajax接收只需要的返回值????

52,797

社区成员

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

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