Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>მთავარი</title>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
- <link rel="stylesheet" type="text/css" media="screen" href="simplmb.css" />
- <script src="simplmb.js"></script>
- </head>
- <body>
- <form id="login" action="/login" method="POST">
- <label>
- Username:
- <input type="text" name="username" value="">
- </label>
- <label>
- Password:
- <input type="password" name="password" value="" autocomplete="off">
- </label>
- <button type="submit">Login</button>
- <br/>
- <div class="error"></div>
- </form>
- <script type="text/javascript">
- $(function() {
- var $loginForm = $('form#login');
- // handle form submit event
- $loginForm.on('submit', function(e) {
- e.preventDefault(); // prevent browser doing default post
- $.ajax({
- type: $loginForm.attr('method'), // get method from form tag
- url: $loginForm.attr('action'), // get action url from form tag
- contentType: 'application/json', // telling to server that will send json data
- data: JSON.stringify($loginForm.serializeArray()), // sending object with fields of form
- // something went wrong
- error: function(xhr, textStatus, error) {
- try {
- var response = JSON.parse(xhr.responseText);
- if (response.message) { // if json response has message field
- $loginForm.find('.error:first').html(response.message);
- }
- }
- catch (error) { // tried to parse response string as json, but could not - then just put error string to div
- $loginForm.find('.error:first').html(xhr.responseText);
- }
- },
- success: function(response) {
- if (response.redirectTo) {
- window.location.href = response.redirectTo;
- }
- }
- });
- });
- });
- </script>
- <a href="/reg">signup</a>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement