Olzhas
Olzhas

Reputation: 235

javascript send form to spring security validation

I am need to submit an form through an link tag to spring security validation, but that is not working. Code is below:

<html>
<body>
    <form id="login" method="post" action="/j_spring_security_check">
        <fieldset>
            <input id="username" name="j_username" type="text"/>
            <input id="password" name="j_password" type="password"/>
        </fieldset>
        <fieldset>
            <input type="submit" value="Submit"/>
            <a href="" onclick="postForm();">Demo</a>
        </fieldset>
    </form>
<script type="text/javascript">
    function postForm()
    {
        document.getElementById("username").innerHTML="demo";
        document.getElementById("password").innerHTML="demo";
        document.getElementById("login").submit();
    }
</script>
</body>
</html>

Here when user click Demo link it should authorise and divert to demo page, but its not posting to spring security, while I submit through button is working. How can I authorize through an HTML link?

Upvotes: 0

Views: 835

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388356

You need to prevent the default action of the anchor element

<a href="#" onclick="return postForm();">Demo</a>

And

<script type="text/javascript">
    function postForm()
    {
        document.getElementById("username").value="demo";
        document.getElementById("password").value="demo";
        document.getElementById("login").submit();
        return false;
    }
</script>

Demo: Plunker

Upvotes: 1

Related Questions