【发布时间】:2011-09-28 07:35:45
【问题描述】:
单击提交按钮后,在名称文本框中键入的名称将打印在 servlet 中。但是 servlet 发送的响应并没有返回给客户端。我不确定我哪里出错了。需要帮忙。
JQuery-Ajax 我正在调用我的 sampleServlet。
HTML 页面
<script>
$(document).ready(function(){
$(".Submit").click(function(){
nameVal=$("#name").val();
alert(nameVal ) ;
$.get("http://localhost:8080/dummyService/SampleServlet", {name:nameVal}, function(data) {//This function is supposed to be called once the servlet send the response
alert(data) ;
$("#flag").html(data) ;
});
});
});
</script>
<form id="sampleform" method="POST">
<center>
Enter your Name: <input id="name" class="name" type="text"> <br/><br/>
<input class="Submit" name="Submit" type="button" value="Submit" id="Submit">
</center>
</form>
<div id="flag"> </div>
**SampleServlet**
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("INSIDE DO GET");
String name = request.getParameter("name");
System.out.println(name);
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println("Hello " + name);
out.flush();
out.close();
}
【问题讨论】:
-
试试
$.get("/dummyService/SampleServlet" -
也可以在萤火虫中看到错误
-
是在 8080 端口上调用 servlet 的页面,或者可能在 apache 的 80 端口上,因为这可能是个问题。
-
@3nigma,@stivlo 实际上 html 页面不在服务器代码库中。将页面放入服务器并更改为 $.get("/dummyService/SampleServlet") 后工作正常。它有什么不同?
-
@james007 在答案中解释
标签: javascript servlets jquery response