如何比较jsp页面和ajax页面的响应?

问题描述 投票:0回答:2

我正在比较从jsp page到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");
   }

   %>
javascript ajax jsp jquery-ui servlets
2个回答
0
投票

我怀疑它与.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();
  }
});

[在大多数情况下,除非Web服务器出现错误,否则AJAX不会error,因此会出现401、404或500错误。因此,我们想确保我们正在success中完成大部分工作,当我们获得200状态时会触发该工作。

所以一些文本包回来了。根据您的Java,它可以是successinvalid combination of email and passwordthis account does not have information。我们现在可以看到该软件包在控制台中的作用。然后,我们需要对其进行测试。

由于这是针对AJAX的,所以没有办法为我进行测试。通常最好使用最简单的逻辑测试。因此,!= -1会起作用,但是> -1一样好,而且不会造成混乱。


-1
投票

您可以尝试这样的事情

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();
}

我只是不理解您在代码中面临的问题,但是以这种格式尝试可能会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.