【问题标题】:Ajax XMLHttpRequest invokes servlet doFilter or notAjax XMLHttpRequest 是否调用 servlet doFilter
【发布时间】:2014-02-14 05:30:02
【问题描述】:

我正在尝试来自 jsp 页面的 ajax 调用,如下所示,

 <head>
    <script>
        function loadXMLDoc() {
            var xmlhttp;
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.open("GET", "ajax_info.txt", true);
            xmlhttp.send();
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    </script>
    </head>
    <body>

        <button type="button" onclick="loadXMLDoc()">Change Content</button>

    </body>

这里第一次调用 servlet 过滤器,但在 ajax 调用期间我看不到调用 doFilter。

过滤器的 url-mapping 被映射到服务器的所有传入请求为 *。

为什么这里没有为 Ajax 调用调用 Servlet 过滤器?

【问题讨论】:

  • xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("myDiv").innerHTML = xmlhttp.responseText; } } 尝试从内部就绪状态更改写入响应文本

标签: java javascript jquery ajax servlets


【解决方案1】:
 <head>
    <script>
        function loadXMLDoc() {
            var xmlhttp;
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                }
            xmlhttp.open("GET", "ajax_info.txt", true);
            xmlhttp.send();

        }
    </script>
    </head>
    <body>

        <button type="button" onclick="loadXMLDoc()">Change Content</button>

    </body>

使用onreadystatechange 函数。

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2021-02-03
    • 2011-04-09
    相关资源
    最近更新 更多