简体   繁体   English

JSP没有收到来自Servlet的AJAX响应

[英]JSP not receiving AJAX response from Servlet

I have a simple jsp page that sends an ajax request to a servlet and awaits a response. 我有一个简单的jsp页面,该页面将ajax请求发送到servlet并等待响应。 Unfortunately, it's not receiving the response after many attempts. 不幸的是,经过多次尝试,它没有收到响应。 The ajax requests are being transmitted but response isn't being received. Ajax请求正在发送,但未收到响应。

As of now, I have a dropdown in my page and a textbox. 到目前为止,我的页面上有一个下拉列表和一个文本框。 I am trying to get the value selected in the dropdown to be printed in the textbox on the dropdown's "onchange" event. 我试图获取下拉菜单中选择的值,以将其打印在下拉菜单的“ onchange”事件上的文本框中。 Here's my code. 这是我的代码。 Any help regarding this would be very welcome. 任何对此的帮助将非常欢迎。

JSP PAGE JSP页面

 <script>            
 function ajaxrequest(){ return new XMLHttpRequest();}        
 function ac()
   {
     var myajaxrequest = new ajaxrequest();
     if(myajaxrequest==null){alert("AJAX NOT WORKING");}         
     else
       {
         alert("AJAX WORKING");                 
         var ind2=document.getElementById("dd1").value;
         myajaxrequest.onreadystatechange=connection;                   
         myajaxrequest.open("GET", "../ajaxservlet?dd1="+ind2,true );
         myajaxrequest.send(null);
       }
    }

 function connection()  
   {               
     if(myajaxrequest.readyState==4)  
        {  
          var x=myajaxrequest.responseText; 
          document.getElementById("result").innerHTML = x;
        }  
   }  
 </script>
 <body>
   <form id = "form1" name ="form1" >
      <select id="dd1" name="dd1" onchange= "ac()">
        <option>Please select </option>
        <option>ankur</option>
        <option>akshay</option>
      </select>
      <input type="text" id="result" name="result" /> 
   </form>
 </body>

SERVLET : SERVLET:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws     ServletException, IOException {

  System.out.println("hitting servlet");
  String abc = request.getParameter("dd1");
  System.out.println(abc);
  PrintWriter pw=response.getWriter();  
  response.setContentType("text/html");  
  pw.println(abc);
  }

The values selected in the dropdown are getting printed in the console but aren't being transmitted back. 在下拉列表中选择的值将在控制台中打印,但不会被传输回去。 Thanks for your time. 谢谢你的时间。

myajaxrequest is local to the ac() function since you've used the var keyword. 自从使用var关键字以来, myajaxrequest对于ac()函数myajaxrequest是本地的。 You cannot access it in the connection() function. 您不能在connection()函数中访问它。

Try declaring your connection() function within ac() itself. 尝试在ac()本身内声明您的connection()函数。

Instead of checking the status of readyState , using XHR level 2, you can simply attach a handler to the onload event. 无需使用XHR级别2检查readyState的状态,只需将处理程序附加到onload事件即可。

BTW, if native XHR isn't supported ajaxrequest() will throw an error; 顺便说一句,如果不支持本机XHR ajaxrequest()会抛出错误; it won't return null. 它不会返回null。

尝试全局初始化myajaxrequest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM