设置jsp上的Ajax响应不起作用

Set Ajax response on jsp not working

本文关键字:响应 不起作用 Ajax 上的 jsp 设置      更新时间:2023-09-26

过去几个小时我试图解决这个问题,但我不能。我正在使用jquery发送ajax请求,并根据响应在jsp上设置数据。实际上我正在检查登录细节如果登录失败它会在标签上设置错误信息问题是错误信息会设置几秒钟然后删除我的意思是错误信息会设置几秒钟如果登录失败信息会设置在标签上用户输入有效的详细信息

thanks in advance

我的代码

         LoginDemo.jsp
      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN  "http://www.w3.org/TR/html4/loose.dtd">
  <html>
   <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <link href="css/firstpage.css" rel="stylesheet" type="text/css" /><!-- <style> -->
    <script src="js/jquery.min.js"></script>
    <title>Insert title here</title>
 <script>
   $(document).ready(function(){
   $("#submit").click(function(){   
  $.ajax({  
    type: "POST",
    url: "AddInspServlet",
    cache:false,
    data: { userid: $('#username').val(), password: $('#password').val(),btn:$('#submit').val() },
     success:function(data,textstaus){
     alert("success:"+data);     
     if(data == "no"){       
        alert( document.getElementById("error").innerHTML);     
         document.getElementById("error").innerHTML= "UserName OR Password is  incorrect";
        alert( document.getElementById("error").innerHTML);
  }else{
      location.href = 'Home.jsp';
  }
   },
   error:function(data){
    alert("error"+data);
     },  
    });
    });
    });
  </script>
  </head>
   <body>
   <form id="login" name="Loginform" method="post">
     <h1><b>Log In</b></h1>
       <fieldset id="inputs">
       <input id="username" type="text" placeholder="Username" autofocus required>   
       <input id="password" type="password" placeholder="Password" required>
    </fieldset>
     <fieldset id="actions">
      <input type="submit" id="submit" value="login">
      <a href="">Forgot your password?</a>
     </fieldset>
   <label id="error" style="color: red;font: bolder; font-size: 18px;"> 
   </label>
  </form>
</body>

      AddInspServlet

这里我添加了服务器执行和检索响应的代码

   if(btn.equals("login"))
     {
    System.out.println("***** login condition checking ******");
    String password =request.getParameter("password");
    UserVO v =op.loginCheck(username, password);
    if(password.equals(v.getPassword()))
    {
        System.out.println("inside true condition");
        HttpSession session = request.getSession();
        session.setAttribute("userid",v.getUser_id());
        session.setAttribute("username",v.getUsername());
        session.setAttribute("user", v.getFirst_name()+" "+v.getLast_name());
        session.setAttribute("roleid", v.getRole_id());
      //  response.sendRedirect("Home.jsp");
        System.out.println("submitting data success fully");
        out.print("yes");
        }
        else
        {
        System.out.println("false condition");
        out.print("no");
       }
     }

去掉form标签

通过在form标签中添加type="submit" input元素而不使用action属性,页面将被重新加载。

或者您可以保留form标签,并将submit按钮的类型更改为type="button"。然后,表单将不会被执行,页面也不会重新加载。

如果你能更具体地说明问题就更好了。我想你的问题是标签显示几秒钟,然后消失。这是真的吗?然后尝试返回"false"在你的数据== "no"逻辑。

谢谢。