开发者

JQuery Ajax call not getting the respose from the servlet

开发者 https://www.devze.com 2023-04-07 23:51 出处:网络
Once the submit button is clicked, The name typed on the name textbox is printed in the servlet . But the response send by the servlet is not coming back to the client. I am not开发者_如何学C sure whe

Once the submit button is clicked, The name typed on the name textbox is printed in the servlet . But the response send by the servlet is not coming back to the client. I am not开发者_如何学C sure where i am going wrong. Need help.

JQuery-Ajax i am calling my sampleServlet.

HTML PAGE

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


The HTML page containing the JavaScript and your servlet must be on the same hostname and port because of the same origin policy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号