开发者

.ajax() method is not working

i am new to Jquery and ajax, wrote a simple jsp page which will show the text on each onkeyup event, the request made by ajax will go through AjaxServlet . i am not getting output with .ajax() method while if i use plain ajax using XhtmlRequest it gives the output. please as sson as possible

****JSP Page*******

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
 function showName(str){



        $.ajax({
              url: '/AjaxServlet',
              type开发者_高级运维: 'GET',
               data: 'name='+name,
              success: function(response){
               $("#showName").html(response);

                  }

        });



}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input type="text" onkeyup="showName(this.value)">Enter your name</input>
<p>Suggestions: <span id="showName"></span></p> 

</body>
</html>

****AjaxServlet********

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        response.setContentType("text/xml");
        PrintWriter responseWriter=response.getWriter();
        responseWriter.write("Welcome "+name);

    }


Where is your jQuery library? Have you forgotten to download and include it in the HTML page?


is 'name' actually the correct thing to use? the function is showName(str)?

try the following:

    $.ajax({
          url: '/AjaxServlet',
          type: 'GET',
          data: {name:str}, // use a object literal here
          success: function(response){
             $("#showName").html(response);
          },
          error: function(xhr, status, error){

             alert("help I died! [" + status + "] [" + error +"]" ); 
          }
    });


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException {
    String name=request.getParameter("name");
    // your output isn't xml! ... use text/plain in stead
    response.setContentType("text/plain");
    PrintWriter responseWriter=response.getWriter();
    responseWriter.write("Welcome "+name);

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜