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.
精彩评论