javajavascriptjquerysecurityjcryption

JQuery form on submit calling recursively


I am doing encryption for username and password in JavaScript using Jcryption algorithm. and decryption using Java . in that process before encryption process completion in JavaScript form is submitted .In Java I am not getting encrypted username and passwords. and I am getting normal values so I am prevented form submit and written code in the form submit after the encryption process completed I am calling submit event but form submitting multiple times. give me suggestions is there any way to solve issues .or any alternate way to solve issues.

Thanks In Advance

My Html code

    <form name="frm" method="post" action="/xsl-portal/xlogin" onSubmit="" target="_parent"  autocomplete="off"> 
       <input type="text" maxlength="45"  name="eid1" id="eid1" />
       <input type="hidden" maxlength="45" id="eid" name="eid" />
       <input type="password" id="pw" name="pw" />
       <input type="image" src="home-images/button-submit.png" id="submitButton" value="Submit" name="image"  onclick="submitButtonClick();" />

<input type="hidden" value="" name="submit" id="submit"/> 
    </form>
Javascript code
    var keys='';

    function getKeys() {
        $.jCryption.getKeys("/sakai-login-tool/EncryptionServlet?generateKeypair=true", function(
            receivedKeys) {
            keys = receivedKeys;        

        });
    }
    getKeys();
    var encryptedUser='';
    var encryptedPassword='';   

/* form submit code*/

// $('#submitButton').on('click',function(){
            function submitButtonClick(){ 



            }
        // });

$('form').submit(function(e) {
               e.preventDefault();
                var user=$('#eid1').val();
                    var password=$('#pw').val();

                    encryptionProcess(user,password).pipe(applyValues).pipe(function(){

                        console.log("eid"+$('#eid').val());
                    console.log("pw"+$('#pw').val());
                    $('form').submit();
                    });
    })

;
    function encryptionProcess(user,password){
        var d= $.Deferred();
        setTimeout(function() {
            $.jCryption.encrypt(user, keys, function(encrypted) {
                encryptedUser = encrypted;
                console.log("printing user encryption:"+encryptedUser);
            });
            $.jCryption.encrypt(password, keys, function(encryptedPasswd) {
                encryptedPassword = encryptedPasswd;                    /**
                 * As both userName and password are encrypted now Submit login
                 */
                 console.log("printing password encryption:"+encryptedPassword);
                });
              d.resolve();
        }, 10);
    return d.promise();         
    }
    function applyValues(){
        var d1= $.Deferred();
        setTimeout(function() {
            $('#eid').val(encryptedUser);
            $('#pw').val(encryptedPassword);
          d1.resolve();
        }, 10);
        return d1.promise();
    }

Solution

  • It is because of $('form').submit();. Since you are calling the jQuery submit() method, it will call the associated submit event handler causing a recursive cycle

    Change it to use the dom element's submit() method, which will not result in the invocation of submit event handlers.

    $('form')[0].submit();
    //or e.target.submit();