关于ajax和jQuery的问题

快进到十一月 2019-04-25 09:51:59
我现在的问题就是ajax访问不到Servlet,求大佬们帮我看看


$(document).ready(function(){
$('#login').click(function(){
var username = $("#username").val();
var password = $("#password").val();
var code = $("#code").val();
if(username == ""){
$("#username").focus;
return;
}
if(password == ""){
$("#password").focus;
return;
}
if(code == ""){
$("#code").focus;
return;
}
$.ajax({
url:"/LoginServlet?method=doLogin", //这里的路径是对的
type:"get",
data:{"username":username,"password":password,"code":code},

beforeSend:function(){
$("#login").val("登陆中");
},

success:function(data){
if(data==false){
$("#password").val("");
$("#password").attr("placeholder","密码错误");
$("#login").val("登录");
}else{
window.location.href="main.jsp";
}
},
});
});
});



<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE>
<html>
<head>
<title>登录</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link href="https://fonts.font.im/css?family=Permanent+Marker" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="/hon/js/user.js"></script>
<link rel="stylesheet" type="text/css" href="/hon/css/login.css">
</head>
<body>
<div class="login-form">
<h1>Log In</h1>
<input type="text" value="${param.username}" value="" class="txtb" id="username" placeholder="用户名">
<input type="password" value="${param.password}" value="" class="txtb" id="password" placeholder="密码">
<table>
<tr>
<td><input type="text" value="${param.code}" value="" class="txtb" id="code" maxlength="4" placeholder="验证码"></td>
<td><img border=0 style="width: 80px;margin-top:20px" src="checkcodeservlet"/></td>
</tr>
</table>
<input type="button" id="login" class="login-btn" value="登录">
<a href="/hon/register.jsp">还未注册?</a>
<a href="/hon/findpwd.jsp"> 忘记密码?</a>
</div>
</body>
</html>



public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
System.out.println("TEST"); //单击登录后TEST没有被打印出来
String methodname = req.getParameter("method");
try {
Class clazz = Class.forName("com.douban.controll.LoginServlets");//获取到UserServlet类
Method[] methods = clazz.getDeclaredMethods();//然后把类的方法一块一块的分出来
for (Method method : methods) {
String name = method.getName(); //遍历类的方法,如果和传入的方法名相同就调用此方法
if (name.equals(methodname)) {
method.invoke(clazz.newInstance(), req, resp);
break;
}
}
} catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

protected void doLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//获取请求信息
String username = req.getParameter("username");
String password = req.getParameter("password");
String code = req.getParameter("code");
String rcode = (String) req.getSession().getAttribute("code");
UserService us = new UserServiceImpl();
if(!code.equals(rcode)) {
out.print(false);
}
if(us.login(username, password)) {
req.setAttribute("username", username);
out.print(true);
}else {
out.print(false);
}
out.flush();
out.close();

}
}

...全文
185 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
快进到十一月 2019-04-25
  • 打赏
  • 举报
回复
引用 7 楼 情非彼岸花 的回复:
success:function(data){ if(data==false){ $("#password").val(""); $("#password").attr("placeholder","密码错误"); $("#login").val("登录"); }else{ window.location.href="main.jsp"; } }, 把你这个逗号去掉 后面又没得别的参数了;
还是不行
咦哟~~~ 2019-04-25
  • 打赏
  • 举报
回复
引用 6 楼 计院库里 的回复:
引用 5 楼 情非彼岸花 的回复:
[quote=引用 3 楼 计院库里 的回复:] [quote=引用 1 楼 情非彼岸花 的回复:] 调试下吧,没有进入ajax里面去,是格式错误还是什么的。
我在火狐里面调试但是看不出什么,应该看哪里
我一般用谷歌的 这个你百度百度吧,我看了下 你代码,你把focus,改成focus();再试试看 [/quote]还是没进到后台 不知道是不是路径的问题[/quote] 路径问题就给你报404了,你这应该是点了 没反应的;
咦哟~~~ 2019-04-25
  • 打赏
  • 举报
回复
success:function(data){ if(data==false){ $("#password").val(""); $("#password").attr("placeholder","密码错误"); $("#login").val("登录"); }else{ window.location.href="main.jsp"; } }, 把你这个逗号去掉 后面又没得别的参数了;
快进到十一月 2019-04-25
  • 打赏
  • 举报
回复
引用 5 楼 情非彼岸花 的回复:
引用 3 楼 计院库里 的回复:
[quote=引用 1 楼 情非彼岸花 的回复:] 调试下吧,没有进入ajax里面去,是格式错误还是什么的。
我在火狐里面调试但是看不出什么,应该看哪里
我一般用谷歌的 这个你百度百度吧,我看了下 你代码,你把focus,改成focus();再试试看 [/quote]还是没进到后台 不知道是不是路径的问题
咦哟~~~ 2019-04-25
  • 打赏
  • 举报
回复
引用 3 楼 计院库里 的回复:
引用 1 楼 情非彼岸花 的回复:
调试下吧,没有进入ajax里面去,是格式错误还是什么的。
我在火狐里面调试但是看不出什么,应该看哪里
我一般用谷歌的 这个你百度百度吧,我看了下 你代码,你把focus,改成focus();再试试看
快进到十一月 2019-04-25
  • 打赏
  • 举报
回复
引用 2 楼 情非彼岸花 的回复:
[quote=引用 楼主 计院库里 的回复:] 我现在的问题就是ajax访问不到Servlet,求大佬们帮我看看

$(document).ready(function(){
	$('#login').click(function(){
		var username = $("#username").val();
		var password = $("#password").val();
		var code = $("#code").val();
		if(username == ""){
			$("#username").focus;
			return;
		}
		if(password == ""){
			$("#password").focus;
			return;
		}
		if(code == ""){
			$("#code").focus;
			return;
		}
		$.ajax({
			url:"/LoginServlet?method=doLogin",  //这里的路径是对的
			type:"get",
			data:{"username":username,"password":password,"code":code},

			beforeSend:function(){
				$("#login").val("登陆中");
			},
			
			success:function(data){
				if(data==false){
					$("#password").val("");
					$("#password").attr("placeholder","密码错误");
					$("#login").val("登录");
				}else{
					window.location.href="main.jsp";
				}
			},
		});
	});
});

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE>
<html>
<head>
<title>登录</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link href="https://fonts.font.im/css?family=Permanent+Marker" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="/hon/js/user.js"></script>
<link rel="stylesheet" type="text/css" href="/hon/css/login.css">
</head>
<body>
	<div class="login-form">
		<h1>Log In</h1>
			<input type="text" value="${param.username}" value="" class="txtb" id="username" placeholder="用户名">
			<input type="password" value="${param.password}" value="" class="txtb" id="password" placeholder="密码">
			<table>
				<tr>
					<td><input type="text" value="${param.code}" value="" class="txtb" id="code" maxlength="4" placeholder="验证码"></td>
					<td><img border=0 style="width: 80px;margin-top:20px" src="checkcodeservlet"/></td>
				</tr>
			</table>
			<input type="button" id="login" class="login-btn" value="登录">
			<a href="/hon/register.jsp">还未注册?</a>
			<a href="/hon/findpwd.jsp"> 忘记密码?</a>
	</div>
</body>
</html>

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		System.out.println("TEST"); //单击登录后TEST没有被打印出来
		String methodname = req.getParameter("method");
		try {
            Class clazz = Class.forName("com.douban.controll.LoginServlets");//获取到UserServlet类
            Method[] methods = clazz.getDeclaredMethods();//然后把类的方法一块一块的分出来
            for (Method method : methods) {
                String name = method.getName();    //遍历类的方法,如果和传入的方法名相同就调用此方法
                if (name.equals(methodname)) {
                    method.invoke(clazz.newInstance(), req, resp);
                    break;
                }
            }
        } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
	
	protected void doLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		PrintWriter out = resp.getWriter();
		//获取请求信息
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String code = req.getParameter("code");
		String rcode = (String) req.getSession().getAttribute("code");
		UserService us = new UserServiceImpl();
		if(!code.equals(rcode)) {
			out.print(false);
		}
		if(us.login(username, password)) {
			req.setAttribute("username", username);
			out.print(true);
		}else {
			out.print(false);
		}
		out.flush();
		out.close();
		
	}
}
focus是个函数 focus() 你写法有问题,它不会进去ajax方法的;[/quote]我调试的时候前面都通过了,就是到$.ajax这里好像就有问题。不过是不是只要ajax中有某个地方写法错了就都不会运行?
快进到十一月 2019-04-25
  • 打赏
  • 举报
回复
引用 1 楼 情非彼岸花 的回复:
调试下吧,没有进入ajax里面去,是格式错误还是什么的。
我在火狐里面调试但是看不出什么,应该看哪里
咦哟~~~ 2019-04-25
  • 打赏
  • 举报
回复
引用 楼主 计院库里 的回复:
我现在的问题就是ajax访问不到Servlet,求大佬们帮我看看

$(document).ready(function(){
	$('#login').click(function(){
		var username = $("#username").val();
		var password = $("#password").val();
		var code = $("#code").val();
		if(username == ""){
			$("#username").focus;
			return;
		}
		if(password == ""){
			$("#password").focus;
			return;
		}
		if(code == ""){
			$("#code").focus;
			return;
		}
		$.ajax({
			url:"/LoginServlet?method=doLogin",  //这里的路径是对的
			type:"get",
			data:{"username":username,"password":password,"code":code},

			beforeSend:function(){
				$("#login").val("登陆中");
			},
			
			success:function(data){
				if(data==false){
					$("#password").val("");
					$("#password").attr("placeholder","密码错误");
					$("#login").val("登录");
				}else{
					window.location.href="main.jsp";
				}
			},
		});
	});
});

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE>
<html>
<head>
<title>登录</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link href="https://fonts.font.im/css?family=Permanent+Marker" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="/hon/js/user.js"></script>
<link rel="stylesheet" type="text/css" href="/hon/css/login.css">
</head>
<body>
	<div class="login-form">
		<h1>Log In</h1>
			<input type="text" value="${param.username}" value="" class="txtb" id="username" placeholder="用户名">
			<input type="password" value="${param.password}" value="" class="txtb" id="password" placeholder="密码">
			<table>
				<tr>
					<td><input type="text" value="${param.code}" value="" class="txtb" id="code" maxlength="4" placeholder="验证码"></td>
					<td><img border=0 style="width: 80px;margin-top:20px" src="checkcodeservlet"/></td>
				</tr>
			</table>
			<input type="button" id="login" class="login-btn" value="登录">
			<a href="/hon/register.jsp">还未注册?</a>
			<a href="/hon/findpwd.jsp"> 忘记密码?</a>
	</div>
</body>
</html>

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		System.out.println("TEST"); //单击登录后TEST没有被打印出来
		String methodname = req.getParameter("method");
		try {
            Class clazz = Class.forName("com.douban.controll.LoginServlets");//获取到UserServlet类
            Method[] methods = clazz.getDeclaredMethods();//然后把类的方法一块一块的分出来
            for (Method method : methods) {
                String name = method.getName();    //遍历类的方法,如果和传入的方法名相同就调用此方法
                if (name.equals(methodname)) {
                    method.invoke(clazz.newInstance(), req, resp);
                    break;
                }
            }
        } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
	
	protected void doLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		PrintWriter out = resp.getWriter();
		//获取请求信息
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String code = req.getParameter("code");
		String rcode = (String) req.getSession().getAttribute("code");
		UserService us = new UserServiceImpl();
		if(!code.equals(rcode)) {
			out.print(false);
		}
		if(us.login(username, password)) {
			req.setAttribute("username", username);
			out.print(true);
		}else {
			out.print(false);
		}
		out.flush();
		out.close();
		
	}
}
focus是个函数 focus() 你写法有问题,它不会进去ajax方法的;
咦哟~~~ 2019-04-25
  • 打赏
  • 举报
回复
调试下吧,没有进入ajax里面去,是格式错误还是什么的。
快进到十一月 2019-04-25
  • 打赏
  • 举报
回复
大佬们帮我看看吧
咦哟~~~ 2019-04-25
  • 打赏
  • 举报
回复
我这边调试 都已经到404了,证明ajax走通了,只是没有找到servlet方法而已,你看看你的页面调试咋样了,我也刚学。。。,不过你总不会说servlet没有注册吧。。。
咦哟~~~ 2019-04-25
  • 打赏
  • 举报
回复
调试下页面吧,会给你报错的。

52,797

社区成员

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

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