0

I am developing a sample application with jquery ajax.My Login.jsp has

       <html>
       <script>
    $(document).ready(function() {
    $("#dialog").dialog();

    $("#LoginForm").submit(function()
    {
     var username=$("#username").val();
     var password=$("#password").val();
     var datastring='username '+username+ '&password '+password;

    $.ajax({
        url: 'Login',
        type: "POST",
        data: datastring,
       /*error: function(){
            alert("Data Error"); 
        },*/
        success: function (data){
            window.open.href("success.jsp");
        }
     });
    });
});
 </script> 
 </head>
 <body>
<div id="dialog" title="LoginDB">
<form id="LoginForm" method="post">
<fieldset>
<label>Username:</label>
<input type="text" id="username" value=""></input><br></br>
<label>Password:</label>  
<input type="password" id="password" value=""></input><br></br>
<input type="submit" id="submit" class="submit" value="Log In" align="middle"></input>
</fieldset>
</form>
</div>
</body>
</html>

when I run the application I see that null value is passed in servlet where I check for credentials and if success the data is inserted and the user is redirected to success page.I know its a simple error but am not able to figure it out..

Can anyone point me out??Thanks:)

1 Answer 1

5

The querystring that is being generated is wrong.

Change var datastring='username '+username+ '&password '+password; to

var datastring='username='+username+ '&password='+password; 

Better alternative is to use the jquery form serialize to get the post data.

Try this:

var postData =$("#LoginForm").serialize();      
$.ajax({         
 url: 'Login',         
 type: "POST",         
 data: postData
.
.
.
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was going to point out as well, alternatively var datastring = {'username': username, 'password': password}; should also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.