Prototype的Ajax.Request和Internet Explorer 8

时间:2009-07-14 01:13:49

标签: javascript ajax internet-explorer internet-explorer-8 prototypejs

以下代码应该在页面加载后执行AJAX请求,然后在弹出窗口中显示该AJAX调用的响应。这适用于Firefox,但我不知道为什么它在IE8中不起作用。

<html>
<head>
    <script type="text/javascript" src="js/prototype.js"></script>
    <script type="text/javascript">
        // Do this stuff when DOM finishes loading.
        document.observe("dom:loaded", function() {
            new Ajax.Request("page.html", {
                onSuccess: function(response) {
                    alert(response.responseText);
                }
            });
        }); 
    </script>
</head>
<body>
</body>
</html>

page.html只包含

hello world

我错过了一些明显的东西,或者这是原型js库与IE8完全兼容的结果?我尝试了最新的稳定原型版本(1.6.0.3)和最新版本(1.6.1 RC3)但没有运气。非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:2)

您是在网站上还是使用文件?我能够在Visual Studio中的网站上运行您的代码,但是当我从文件中尝试它时,我收到了“拒绝访问”错误。我怀疑由于IE8中的安全限制,它无法对本地文件系统中的文件执行Ajax请求。

这是我在Default.aspx页面中用于加载page.htm文件的确切代码。请注意,我更改了方法 - 因为它不是表单 - 我添加了失败和异常的回调。异常回调是我用本地文件触发的内容。正如我所说,它在访问同一网站中的页面时工作正常。

<script type="text/javascript">
    document.observe( 'dom:loaded', function() {
        new Ajax.Request("page.htm", {
            method: 'get',
            onSuccess: function(response) {
                alert(response.responseText);
            },
            onFailure: function(response) {
                alert(response);
            },
            onException: function(request,error) {
                alert(error.message);
            }
        }); 
    });
</script>

答案 1 :(得分:2)

现在回答一下,只是想提一下,当你在Ajax调用中使用'get'时,IE8会做一些疯狂的缓存。我的Web应用程序在FF中运行良好,但在IE8中,一些ajax调用从未进行过。我改变了方法:从'get'到'post',一切都很好。

method: 'post'

另外,为了进一步确保IE8中没有会话变量的搞笑业务,请将以下元标记放在html页面的头部。

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">