I have a small web project assembled in maven
, it has a simple login window that accesses the servlet using an ajax
request. The problem is that the request fails, the servlet is not called. However, if you directly access the servlet, it works as it should (regardless of the request data, the servlet sends a test string to the server). So the reason for the ajax
request.
Window code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Log In</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("#login-button").click(function(){ var userPassword = $("input#userPassword").val(); var userLogin = $("input#userLogin").val(); var dataString = "log_in " + userLogin + " " + userPassword; $.ajax({ type: "POST", url: "/login", data: dataString, success: function(data) { alert("Successful request"); alert(data); }, error: function() { alert("Unsuccessful request"); }, dataType: "text" }); }); }); </script> </head> <body> <form class="login-form"> <h1>Login Form</h1> <input id="userLogin" type="text" name="user-login" placeholder="Enter login"/> <input id="userPassword" type="password" name="user-password" placeholder="Enter password"/> <input id="login-button" type="submit" value="Log in"/> </form> </body> </html>
Servlet written in java
, thus registered in web.xml
<servlet> <servlet-name>Login</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
http//server:port/contextPath/login
- will_hunting