简体   繁体   English

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

[英]Set Ajax response on jsp not working

Last few hours i am trying to solve this but i can not.I am sending ajax request using jquery and based on response i set data on jsp.actully i am checking login detail so if login fail it set error message on label problem is that error message is set for few second and removed i mean to say error message is set for few second i want that if login fails the message is set on label still user enter valid details 最近几个小时,我试图解决这个问题,但我不能。我正在使用jquery发送ajax请求,并根据响应我在jsp.actully上设置数据,我正在检查登录详细信息,因此如果登录失败,则会在标签问题上设置错误消息是错误消息设置了几秒钟并删除了我的意思是说错误消息设置了几秒钟,我希望如果登录失败,则该消息设置在标签上,仍然用户输入有效的详细信息

thanks in advance 提前致谢

Here is my code 这是我的代码

         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

Here im adding code which server executed and retrieve response 我在这里添加服务器执行的代码并检索响应

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

Get rid of the form tags. 摆脱form标签。

By adding a type="submit" input element without using the action attribute in the form tag, the page will be reloaded. 通过添加type="submit" input元素而不在form标记中使用action属性,页面将被重新加载。

Or you could keep the form tags and change the type of the submit button to type="button" . 或者,您可以保留form标签,并将submit按钮的类型更改为type="button" The form will then not be executed and the page will not reload. 然后将不执行该表格,并且该页面也不会重新加载。

It would be better if you be more specific about the problem. 如果您对问题更具体,那会更好。 I assume your problem is Label is getting displayed for few seconds and then get disappeared. 我认为您的问题是Label显示了几秒钟,然后消失了。 Is that is true? 是真的吗 then try to return "false" inside you data == "no" logic. 然后尝试在您的data ==“ no”逻辑中返回“ false”。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM