在jquery中从服务器获取响应的问题

时间:2010-05-23 13:28:14

标签: jquery ajax servlets

我正在使用$.ajax(options)方法根据用户名和密码将请求传递给服务器,但每当我尝试通过XMLHttpRequest对象打印响应时,响应成功,我得到一个空值。< / p>

$(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");}});

});
});

这是我在执行查询后在servlet代码中执行的操作:

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();
     }
   }

我可以知道这个问题,我该如何解决?

4 个答案:

答案 0 :(得分:1)

而不是:

alert(request.responseText);

使用:

alert(request);

所以应该是这样的:

$(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;

});
});

答案 1 :(得分:0)

您的成功回调应该有三个参数:

success(data, textStatus, XMLHttpRequest)

你所追求的XMLHttpRequest是第三个参数; data.responseText您实际发现的是空白。

请参阅http://api.jquery.com/jQuery.ajax/

答案 2 :(得分:0)

讨论

我很惊讶其他答案没有为你排序。我已经在下面提供了一个完整的工作示例,以防您的代码中出现了一些非常微妙的错误,但首先,我对您的代码有一些注意事项。

您的代码(重新格式化):

$(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");
            }
        });
    });
});

实施例

客户端:

<!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'></script>
<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>

服务器:

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

答案 3 :(得分:0)

我和你有同样的问题。

就我而言,只需在服务器上设置响应的内容类型即可解决问题。

        response.setContentType("text");

然后在你的ajax jquery函数中,你应该删除行alert(request.responseText); //This is displaying nothing并替换为alert(request);

你的jquery代码应该是这样的:

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

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

});
});

服务器端:

try {
    String user = request.getParameter("j_username");
    String password = request.getParameter("j_password");
    response.setContentType("text");
    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();
     }
   }

我知道这是一个老帖子,但我希望它可以帮助别人!