2010-07-30 45 views
0

謝謝,解決了一個問題,這是下一個問題。作爲一個新手,我仍然遇到一個錯誤,簡單的應用程序無法正常工作。我得到INVALID_STATE_ERR:當我執行req.open(「Get」,url,true)時,DOM異常11。命令位於以下index.html文件中的javascript中。簡單的appl不起作用。我在調試模式下使用chrome,但該應用程序在IE8或FF3上也不起作用。有任何想法嗎?INVALID_STATE_ERR:當我執行req.open(「Get」,url,true)時,DOM異常11。

在WindowsVista上使用Eclipse for J2EE和Java6,Ajax。

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<script type="text/javascript"> 
    var req; 

    function focusIn() { 
     //This is how you comment in javascript portion of code 
     //I will now demonstrate an alert function that calls a messagebox to the field, very useful for debugging 
     //this displays in yoru browser 
     alert('Hey dad this is an alert, this function was called by the onload message of the Body'); 

     //There is even a cooler way, for instance say you wanted to display values 
     var two = 2; 
     var one = 1; 
     var result = two + one; 
     //Display your variable result 
     alert(result); 

     document.getElementById("key").focus(); 

    } 

    function convertToDecimal(){ 
     var key = document.getElementById("key"); 
     var keypressed = document.getElementById("keypressed"); 
     keypressed.value = key.value; 
     // onClick="alert('You clicked the button')" 
     var url = "/AjaxResponseServlet?key=" + escape(key.value); 
     if (window.XMLHttpRequest){ 
      req = new XMLHttpRequest(); 
     } 
     else if (window.ActivateXObject){ 
      req = new ActiveXObject("Microsoft.XMLHTTP") 
     } 
     req.open("Get",url,true); 
     req.onreadystatechange = callback;  
     req.send(null); 
    } 

    function callback() { 
     if (req.readyState==4) { 
      if (req.status == 200){ 
       var decimal = document.getElementById("decimal"); 
       decimal.value = req.responseText; 
      } 
     } 
     clear(); 
    } 

    function clear() { 
     var key = document.getElementById("key"); 
     key.value=""; 
    } 
</script> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Ajax on Java Chapter 2</title> 
</head> 
<body onload="focusIn();" > 
<!-- this is how you comment in HTML Body Portion of the code --> 

<h1> AJAX CHARACTER DECODER </h1> 
<h2> Press a key to find its value. </h2> 
<table> 
    <tr> 
     <td> 
      Enter Key Here -- 
      <input type="text" id="key" name="key" onkeyup="convertToDecimal();" /> 
     </td> 
    </tr> 
</table> 
<br /> 
<table> 
    <tr> 
     <td colspan="5" style="border-bottom:solid black 1px;"> 
      Key Pressed: 
      <input type="text" readonly="readonly" id="keypressed" /> 
     </td> 
     </tr> 
     <tr> 
      <td> Decimal </td> 
     </tr> 
     <tr> 
      <td> 
       <input type="text" readonly="readonly" id="decimal" /> 
      </td> 
    </tr> 
</table> 

<!-- this is how you comment in HTML Body Portion of the code --> 

<h1> AJAX CHARACTER DECODER </h1> 
<h2> Press a key to find its value. </h2> 
<table> 
    <tr> 
     <td> 
      Enter Key Here -- 
      <input type="text" id="key" name="key" onkeyup="convertToDecimal();" /> 
     </td> 
    </tr> 
</table> 
<br /> 
<table> 
    <tr> 
     <td colspan="5" style="border-bottom:solid black 1px;"> 
      Key Pressed: 
      <input type="text" readonly="readonly" id="keypressed" /> 
     </td> 
     </tr> 
     <tr> 
      <td> Decimal </td> 
     </tr> 
     <tr> 
      <td> 
       <input type="text" readonly="readonly" id="decimal" /> 
      </td> 
    </tr> 
</table> 

我當前的web.xml是:

<?xml version="1.0" encoding="UTF-8"?> 

http://java.sun.com/XML/NS/JavaEE的/網絡app_2_5.xsd」 ID = 「WebApp_ID」 版本= 「2.5」> Ajax2 的index.html index.htm的 的index.jsp default.html中 的default.htm 的Default.jsp AjaxResponseServlet AjaxResponseServlet com.example.servlets.AjaxResponseServlet AjaxResponseServlet /AjaxResponseServlet JAMES JAMES com.example.servlets.JAMES JAMES /JAMES 在這裏輸入的代碼

我的servlet代碼是:

​​

/* * 注意到一個字符並將其轉換爲十進制,並在響應中發回 *值。 */ // package com.oreilly.ajax.servlet; //導致錯誤,因此註釋掉

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

公共類AjaxResponseServlet延伸的HttpServlet {

private static final long serialVersionUID = 1L; 

public void doGet(HttpServletRequest req, HttpServletResponse res) 
     throws ServletException, IOException { 

    String key = req.getParameter("key"); 
    if (key != null) { 
     // extract the first character from key 
     int keyInt = key.charAt(0); 
     String decimalString = Integer.toString(keyInt); 
     // setup the response 
     res.setContentType("text/xml"); 
     res.setHeader("Cache-Control", "no-cache"); 
     // write out the response string 
     res.getWriter().write(decimalString); 
    } 
    else { 
     // If key comes back as a null, return a question mark. 
     res.setContentType("text/xml"); 
     res.setHeader("Cache-Control", "no-cache"); 
     res.getWriter().write("?"); 
    } 
} 

}

回答

0

的問題的答案是,當AJAX命令與req.open發送到不正確的位置發生異常DOM 11錯誤命令。這是因爲servlet位於eclipse創建的tomcat實例的根目錄中。當URL的ConvertToDecimal函數的req.open("Get",url,true);調用值被更改爲刪除前綴/字符時,所有工作。 url變量更改爲:var url = "AjaxResponseServlet?key=" + escape(key.value); 或者,url變量也適用於:var url = "/Ajax2/AjaxResponseServlet?key=" + escape(key.value); web.xml文件完全沒有改變,它的當前值爲servlet映射:<servlet-mapping> <servlet-name>AjaxResponseServlet</servlet-name> <url-pattern>/AjaxResponseServlet</url-pattern> </servlet-mapping>

請注意,當您第1行第1列上的瀏覽器出現錯誤,並且您正在進行ajax通信,很可能是未處理的響應不是HTML文件。

相關問題