【问题标题】:JQuery Ajax call not getting the respose from the servletJQuery Ajax 调用未从 servlet 获得响应
【发布时间】: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


【解决方案1】:

由于same origin policy,包含 JavaScript 和您的 servlet 的 HTML 页面必须位于相同的主机名和端口上。

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 2019-12-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    相关资源
    最近更新 更多