开发者

Null value passed to servlets using jquery ajax

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>
&开发者_高级运维lt;/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:)


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
.
.
.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜