jQuery Servlet调用返回错误

时间:2013-07-31 21:02:47

标签: jquery servlets

我做了最简单的jQuery servlet调用:

jQuery:

$.ajax( "LoginServlet" )
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); });

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("Posting");
}

在控制台上,我看到Posting,因此调用了Servlet。但是,我仍然收到“错误”警告。

在其他地方,有人提到了可能的“跨站点脚本”问题。这可能是问题吗?

我的servlet位于:localhost:8080/Test/LoginServlet

1 个答案:

答案 0 :(得分:0)

当您执行请求时,您的servlet不会在控制台中打印任何内容,您必须管理周期请求/响应的响应。

请尝试以下方法:

 // Obtain the out writer stream from the response 
 // object for send a response to the client (the web page)
 PrintWriter out = response.getWriter();
 // Print as response of request Posting
 out.println("Posting");
 // Close the out stream
 out.close();

现在您的Jquery请求将得到响应。