繁体   English   中英

在Opera中关注跨域Ajax

[英]Focus with Cross-domain Ajax in Opera

您将需要Opera 9.62来了解所有内容...因为这是当我进行跨子域JavaScript调用(涉及Ajax)时唯一表现出奇怪的浏览器。 请考虑以下三个简单文件,并将它们放在适当的域中。

foo.example.com foo.html (boo.html iframe的父级)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>foo</title>
<script type='text/javascript'>

    document.domain = 'example.com';

    function sendRequest() {
        window.frames['boo'].sendRequest();
    }

</script> 
<head>
<body>

    <input type="submit" value="sendRequest" onclick="sendRequest();" />

    <iframe name="boo" src="http://boo.example.com/boo.html"></iframe>

</body>
</html>

boo.html在(foo.html的IFRAME) boo.example.com

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>boo</title>
<script type='text/javascript'>

    document.domain = 'example.com';

    function sendRequest() {
        var request = null;

        if (window.XMLHttpRequest) { 
            request = new XMLHttpRequest(); 
        } else { 
            request = new ActiveXObject('Microsoft.XMLHTTP'); 
        }

        if (request) {
            request.open('GET', 'http://boo.example.com/helloworld.php', true); 

            request.onreadystatechange = function() {               
                if (request.readyState == 4) {
                    var result = request.responseText;

                    alert(result);
                }
            }

            request.send('');
        }
    }

</script> 
<head>
<body>
</body>
</html>

boo.example.com helloworld.php

<?php
    echo 'Hello World!';
?>

如果您在Opera以外的浏览器中测试了上述代码(已在v9.62上进行了测试),则它的工作原理就像一个魅力(我已经在Safari,Firefox,Chrome中测试过)。 在Opera中,它不起作用,并且会引发安全冲突消息错误。 有人知道这是怎么回事吗? 我已经找到了解决问题的方法,以后再将其发布在这里( 我也想看看您的解决方法 ),但是我也想了解更多有关该问题的信息-有人可以解释吗?

注意 :我已经在我自己的服务器上设置了所有文件,因此您可以在此处查看

更新 :我刚刚在最新的Opera 10.63上进行了测试,它没有这样的问题 因此,您肯定需要使用Opera v9.62来观察该问题

一些较旧的Opera版本有一个已知的错误,该错误使设置document.domain影响XMLHttpRequest的安全上下文。 因此,在设置document.domain之后,将不再允许脚本从它实际来自的服务器加载内容。

推荐的解决方案是简单地升级到不受该错误影响的版本,但是,如果您绝对需要支持9.6x,则可以轻松检测到异常并退回使用postMessage()进行跨域通信。 (在这样的旧版本中,您需要调用document.postMessage()-在较新的版本中为window.postMessage(),但在HTML5规范的旧版本中最初是在文档中定义的。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM