function login ( ) { var username = document.getElementById ( "form-username" ).value; var password = document.getElementById ( "form-password" ).value; hashedpassword = SHA512 ( password ); var xhr = new XMLHttpRequest ( ); xhr.open ( "POST", 'login', true ); xhr.onreadystatechange = function ( ) { if ( xhr.status == 200 ) { if ( xhr.readyState == XMLHttpRequest.DONE ) { var response = xhr.responseText; response = JSON.parse ( response ); if ( response [ 'status' ] == 200 ) { window.location.href = script_name; } } } else { document.getElementById ( 'login-response' ).innerHTML = JSON.parse ( xhr.responseText ) [ 'error_message' ]; setTimeout( function( ) { document.getElementById ( 'login-response' ).innerHTML = "" }, 1500 ); } } data = new FormData ( ); data.append('username', username); data.append('password', password); data.append('hashedpassword', hashedpassword); xhr.send( data ); } $(function ( ) { $( '.form-group input' ).keyup( function ( e ) { if ( e.keyCode == 13 ) { login(); } }); });