当我点击login.html上的提交按钮时,收到错误,未收到消息。我对此没有太多经验,并且没有任何错误消息指向我修复它的方向。 我有我的login.html
<body>
<div id="log">
<h2>Login</h2>
<form method="post" action="login" class="form-1" accept-charset="utf-8" onsubmit="sendAjax()">
<input type="text" name="username" id="username" placeholder="Username"/><br/>
<input type="password" name="password" id="password" placeholder="Password"/><br/>
<input type="submit" value="Sign In"/>
</form>
</div>
</body>
在我的index.html中被调用为div
$('#login').click(function(e){
$('#map-canvas').addClass("hidden");
$('#dMain').removeClass("hidden");
$('#dMain').load('html/login.html');
e.preventDefault();
我的登录servlet
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
if(databaseConnection.checkUser(username, password))
{
RequestDispatcher rs = request.getRequestDispatcher("Welcome");
rs.forward(request, response);
}
else
{
out.println("Username or Password incorrect");
RequestDispatcher rs = request.getRequestDispatcher("login.html");
rs.include(request, response);
}
}
和我的ajax功能
function sendAjax(){
var article = new Object();
article.username = $('#username').val();
article.password = $('#password').val();
$ajax({
url: "http://localhost:8080/FishingTrax/LoginServlet",
type: 'POST',
dataType: 'json',
data: JSON.stringify(article),
contentType: 'application/json',
mimeType: 'application/json',
success: function (data) {
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
};
答案 0 :(得分:0)
我的代码遇到两个问题,我在sendAjax()之前缺少返回。定义Ajax问题是由于缺少$ ajax中的完整停止所以它应该是$ .ajax
function sendAjax(){
var article = new Object();
article.username = $('#username').val();
article.password = $('#password').val();
$.ajax({
url: "http://localhost:8080/FishingTrax/LoginServlet",
type: 'POST',
dataType: 'json',
data: JSON.stringify(article),
contentType: 'application/json',
mimeType: 'application/json',
success: function (data) {
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
};