views:

50

answers:

4

Hello, I'm using $.ajax(options) method to pass the request to server based on username and password, but whenever I try to print the response by XMLHttpRequest object when response gets successful, I'm getting an empty value.

$(document).ready(function(){
$("#form").submit(function(){
$.ajax({url:"Chat.jsp",type:"POST",data:$("#form").serialize(),success:function(request) {

  alert(request.responseText); //This is displaying nothing
 },error:function(){document.write("YOU can't");}});

});
});

This is what I'm doing in my servlets code after executing query:

try {
    String user = request.getParameter("j_username");
    String password = request.getParameter("j_password");
    if(user != null && password != null) {
         String query = "Select * from users where user_name="+"\'"+user+"\'"+"&& user_pass="+"\""+password+"\"";
         DBCheck db= new DBCheck();
         boolean b = db.doExecuteQuery(con.createStatement(),query);
         response.setHeader("Cache-Control", "no-cache");
         if(b) {

        response.getWriter().println("Username already exits");      
         }
         else {
        response.getWriter().println("Username doesn't exit");
         }  
    }
     }  
     catch(SQLException ex) {
        ex.printStackTrace();
     }
   }

May I know the problem, and how can I fix it?

+1  A: 

Instead of:

alert(request.responseText);

Use:

alert(request);

So it should be like:

$(document).ready(function(){
$("#form").submit(function(){
$.ajax({url:"Chat.jsp",type:"POST",data:$("#form").serialize(),success:function(response) {

  alert(response);
 },error:function(){document.write("YOU can't");}});

 return false;

});
});
Sarfraz
@OP ...and probably call it something less misleading, like `data` or `response`. :-) Details here: http://api.jquery.com/jQuery.ajax/
T.J. Crowder
I'm still getting blank alert dialog box in the above code.
Alvin
@Alvin: what and how are you posting response from your server script?
Web Logic
A: 

Your success callback should have three arguments:

success(data, textStatus, XMLHttpRequest)

The XMLHttpRequest you're after is the 3rd argument; data.responseText is what you're actually finding is blank.

See http://api.jquery.com/jQuery.ajax/

meagar
I've now entered all the three arguments in that method, and now I'm trying to display the message by using alert(data.responseText) but that resulting me an output "undefined". May I know what's the problem now?
Alvin
Can you access the `data.responseXML`?
Lucanos
A: 

Well If you want a response from the server after requesting through POST method we can do something like this

$("#button").click(function() {
          $.post(
                 'thepagetoverfiy.php',
                 { username: $('#userid').val(), password: $('#pass').val() },
                 function(data) {
                      alert(data); // Popups your response 
                 });
});

on the "thepagetoverify.php"

$userid = $_POST['username'];
$password = $_POST['password'];
// Verfiy the User
if($success) { echo "You are successfully logged in."; }
else { echo "There has been an error. Please Try Again"; }
Starx
A: 

Discussion

I'm surprised the other answers haven't sorted it out for you. I've included a complete working example below in case there's just something really subtle going wrong in your code, but first, I have a few notes on your code.

Your code (reformatted):

$(document).ready(function(){
    // Does your form really have `id="form"` in it?
    $("#form").submit(function(){
        $.ajax({
            url:"Chat.jsp",
            type:"POST",
            data:$("#form").serialize(),
            success:function(request) {
                // `request` seems a very strange name; perhaps `data` or `response`?
                alert(request);
            },
            error: function(){
                // You can't use `document.write` after the page has been parsed
                document.write("YOU can't");
            }
        });
    });
});

Example

Client:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Ajax Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'&gt;&lt;/script&gt;
<script type='text/javascript'>
(function() {
    $(document).ready(pageInit);

    function pageInit() {
        $('#btnGo').click(go);
    }

    function go() {
        $.ajax({
            url:        "ajaxserver.jsp",
            type:       "POST",
            data:       {"field1": "one"},
            success:    function(data) {
                alert("Request succeeded: " + data);
            },
            error:      function() {
                alert("Request failed");
            }
        });
    }

})();
</script>
</head>
<body>
<input type='button' id='btnGo' value='Go'>
</body>
</html>

Server:

<%
    response.setContentType("text/plain");
%>Hi there, field1 = <%=request.getParameter("field1")%>.
T.J. Crowder