如何比较 jsp 页面和 ajax 页面的响应?
how to compare response from jsp page to ajax page?
我正在将我从 jsp 页面获得的响应与 ajax 进行比较。我想执行 if out.print() 打印成功消息所以我想在成功 ajax 函数中执行操作。我正在比较数据。但它不工作。我总是去其他部分。我是 ajax 的新人。能帮我解决这个问题吗?
$.ajax({
url: "login.jsp",
//datatype: "text",
type: "POST",
data: datatopost,
success: function(data){
if(data.search("success")!=-1){
console.log(data.data);
$('#loginmessage').html(data);
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
// window.location = "mainpageloggedin.jsp";
//$('#loginmessage').text(data);
}else{
// $('#loginmessage').text(data);
//hide spinner
//window.location = "mainpageloggedin.jsp";
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
}
},
error: function(){
$("#loginmessage").html("<div class='alert alert-danger'>There was an error with the Ajax
Call. Please try again later.</div>");
//hide spinner
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
}
});
jsp代码...
<%@ include file="connection.jsp"%>
<%
String email=request.getParameter("loginemail");
String loginpass=request.getParameter("loginpassword");
ResultSet rs=st.executeQuery("select password from users where email='"+email+"'");
if(rs.next())
{
String password1=rs.getString(1);
if(password1.equals(loginpass))
{
session.setAttribute("email",email);
out.print("success");
}
else
{
out.print("invalid combination of email and password");
}
} else
{out.print("this account does not have information");
}
%>
你能试试这样吗
function crudPost(e) {
e.preventDefault();
let name = document.querySelector('#name').value;
let email = document.querySelector('#email').value;
let parameters = "name=" + name + "&email=" + email;
let xhr = new XMLHttpRequest();
xhr.open('POST', 'process.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
xhr.onload = function () {
if (this.status == 200) {
console.log(this.responseText)//get response from ur jsp
// do stuff
}
let parameters = "name=" + name + "&email=" + email; //pass values when send
}
xhr.send(parameters)
form.reset();
}
我只是不明白你在代码中遇到的问题,但尝试使用这种格式可能会有帮助
我怀疑它与 .search()
或返回的数据类型有关。
首先,始终检查控制台和网络以查看正在为 AJAX 发送和接收哪些数据包。如果您没有得到预期的回报,您可能需要查看您的 Java.
如果您得到了预期的结果,请考虑以下代码。
$.ajax({
url: "login.jsp",
datatype: "text html",
type: "POST",
data: datatopost,
success: function(data){
console.log(data);
if(data.search("success") > -1){
console.log("Found 'success' in AJAX Package.");
$('#loginmessage').html(data);
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
} else {
$("#spinner").css("display", "none");
//show message
$('#loginmessage').html("Login Failed. " + data);
$("#loginmessage").slideDown();
}
},
error: function(x, stat, err){
$("#loginmessage").html("<div class='alert alert-danger'>There was an error with the Ajax. Please try again later. (" + stat +", " + err + ")</div>");
//hide spinner
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
}
});
在大多数情况下,AJAX 不会 error
除非 Web 服务器出现错误,例如 401、404 或 500 错误。所以我们要确保我们在 success
中完成大部分工作,当我们获得 200 状态时会触发它。
所以一些文本包回来了。根据您的 Java,这可能是 success
、invalid combination of email and password
或 this account does not have information
。我们现在可以通过控制台看到那个包是什么。然后我们需要对其进行测试。
因为这是给AJAX的,所以我没法测试。通常最好使用最简单的逻辑测试。所以 != -1
会起作用,但 > -1
也同样好并且不那么令人困惑。
我正在将我从 jsp 页面获得的响应与 ajax 进行比较。我想执行 if out.print() 打印成功消息所以我想在成功 ajax 函数中执行操作。我正在比较数据。但它不工作。我总是去其他部分。我是 ajax 的新人。能帮我解决这个问题吗?
$.ajax({
url: "login.jsp",
//datatype: "text",
type: "POST",
data: datatopost,
success: function(data){
if(data.search("success")!=-1){
console.log(data.data);
$('#loginmessage').html(data);
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
// window.location = "mainpageloggedin.jsp";
//$('#loginmessage').text(data);
}else{
// $('#loginmessage').text(data);
//hide spinner
//window.location = "mainpageloggedin.jsp";
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
}
},
error: function(){
$("#loginmessage").html("<div class='alert alert-danger'>There was an error with the Ajax
Call. Please try again later.</div>");
//hide spinner
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
}
});
jsp代码...
<%@ include file="connection.jsp"%>
<%
String email=request.getParameter("loginemail");
String loginpass=request.getParameter("loginpassword");
ResultSet rs=st.executeQuery("select password from users where email='"+email+"'");
if(rs.next())
{
String password1=rs.getString(1);
if(password1.equals(loginpass))
{
session.setAttribute("email",email);
out.print("success");
}
else
{
out.print("invalid combination of email and password");
}
} else
{out.print("this account does not have information");
}
%>
你能试试这样吗
function crudPost(e) {
e.preventDefault();
let name = document.querySelector('#name').value;
let email = document.querySelector('#email').value;
let parameters = "name=" + name + "&email=" + email;
let xhr = new XMLHttpRequest();
xhr.open('POST', 'process.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
xhr.onload = function () {
if (this.status == 200) {
console.log(this.responseText)//get response from ur jsp
// do stuff
}
let parameters = "name=" + name + "&email=" + email; //pass values when send
}
xhr.send(parameters)
form.reset();
}
我只是不明白你在代码中遇到的问题,但尝试使用这种格式可能会有帮助
我怀疑它与 .search()
或返回的数据类型有关。
首先,始终检查控制台和网络以查看正在为 AJAX 发送和接收哪些数据包。如果您没有得到预期的回报,您可能需要查看您的 Java.
如果您得到了预期的结果,请考虑以下代码。
$.ajax({
url: "login.jsp",
datatype: "text html",
type: "POST",
data: datatopost,
success: function(data){
console.log(data);
if(data.search("success") > -1){
console.log("Found 'success' in AJAX Package.");
$('#loginmessage').html(data);
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
} else {
$("#spinner").css("display", "none");
//show message
$('#loginmessage').html("Login Failed. " + data);
$("#loginmessage").slideDown();
}
},
error: function(x, stat, err){
$("#loginmessage").html("<div class='alert alert-danger'>There was an error with the Ajax. Please try again later. (" + stat +", " + err + ")</div>");
//hide spinner
$("#spinner").css("display", "none");
//show message
$("#loginmessage").slideDown();
}
});
在大多数情况下,AJAX 不会 error
除非 Web 服务器出现错误,例如 401、404 或 500 错误。所以我们要确保我们在 success
中完成大部分工作,当我们获得 200 状态时会触发它。
所以一些文本包回来了。根据您的 Java,这可能是 success
、invalid combination of email and password
或 this account does not have information
。我们现在可以通过控制台看到那个包是什么。然后我们需要对其进行测试。
因为这是给AJAX的,所以我没法测试。通常最好使用最简单的逻辑测试。所以 != -1
会起作用,但 > -1
也同样好并且不那么令人困惑。