2016-01-08 101 views
0

我做了一個簡單的表單,點擊提交後發送到AJAX,處理後發送到servlet。我不打印servlet的響應,只是顯示警報,但不起作用。請任何解決方案。 JSP:爲什麼沒有就緒狀態變爲4?

<script type="text/javascript"> 

      var x = new XMLHttpRequest(); 
      function fn1() 
      { 
       x.onreadystatechange = fn2(); 

       x.open("GET","servlet1",true); 
       x.send(); 

      } 

      function fn2() 
      { 
       alert("happy"); 
       if(x.readyState==4) 
       { 
        alert("happyness"); 
       } 
      } 


     </script> 

的Servlet:

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

    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 

    out.print("hello"); 
} 

Web的XML:

<servlet> 
<description>This is the description of my J2EE component</description> 
<display-name>This is the display name of my J2EE component</display-name> 
<servlet-name>servlet1</servlet-name> 
<servlet-class>ajaxeg2.servlet1</servlet-class> 
</servlet> 

<servlet-mapping> 
<servlet-name>servlet1</servlet-name> 
<url-pattern>/servlet1</url-pattern> 
</servlet-mapping> 
+0

什麼是您的servlet類?是小「s」的「servlet1」嗎? – user0946076422

回答

0

試試這個 `

  var x = new XMLHttpRequest(); 
      x.onreadystatechange = function(){ 
      if(x.readyState == 4 && x.status == 200){ 
       alert("happyness"); 
      } 
      }; 

      x.open("GET","servlet1",true); 
      x.send(); 

你有電話FN1()一次,以ajax呼叫和out.p rint將無法工作,因爲它的ajax請求頁面不能重新加載。

`

+0

雖然沒有幫助謝謝 –

相關問題