【问题标题】:INVALID_STATE_ERR: DOM Exception 11 when I execute the req.open("Get",url,true);INVALID_STATE_ERR: DOM Exception 11 当我执行 req.open("Get",url,true);
【发布时间】:2011-03-23 01:59:39
【问题描述】:

谢谢,解决了一个问题,这是下一个问题。作为这方面的新手,我仍然遇到错误并且简单的应用程序无法正常工作。我得到 INVALID_STATE_ERR: DOM Exception 11 当我执行 req.open("Get",url,true); javascript 中的命令位于以下 index.html 文件中。简单的应用程序不起作用。我在调试模式下的 chrome 中得到了这个,但该应用程序也不能在 IE8 或 FF3 上运行。有什么想法吗?

在 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/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 阿贾克斯2 索引.html 索引.htm 索引.jsp 默认.html 默认.htm 默认.jsp AjaxResponseServlet AjaxResponseServlet com.example.servlets.AjaxResponseServlet AjaxResponseServlet /AjaxResponseServlet 詹姆士 詹姆士 com.example.servlets.JAMES 詹姆士 /詹姆士 在此输入代码

我的 servlet 代码是:

package com.example.servlets;

/* * 获取一个字符并将其转换为十进制并发送回 * 响应中的值。 */ // 包 com.oreilly.ajax.servlet; // 导致错误,所以注释掉

导入 java.io.IOException; 导入 javax.servlet.ServletException; 导入 javax.servlet.http.HttpServlet; 导入 javax.servlet.http.HttpServletRequest; 导入 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("?");
    }
}

}

【问题讨论】:

    标签: java javascript ajax


    【解决方案1】:

    问题的答案是,当使用 req.open 命令将 ajax 命令发送到不正确的位置时发生 DOM 异常 11 错误。这是因为 servlet 位于 eclipse 创建的 tomcat 实例的根目录中。当 ConvertToDecimal 函数的 req.open("Get",url,true); url 调用值更改为删除前缀 / 字符时,一切正常。 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 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-08
      • 2015-05-14
      • 1970-01-01
      • 2016-08-05
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      相关资源
      最近更新 更多