Javascript 关注Opera中的跨域Ajax

Javascript 关注Opera中的跨域Ajax,javascript,ajax,iframe,Javascript,Ajax,Iframe,你需要Opera 9.62来了解这一切。。。因为当我使用Ajax进行跨子域JavaScript调用时,这是唯一一个行为奇怪的浏览器。请考虑以下三个简单文件,并将它们放在适当的域中。 foo.example.com上boo.html iframe的foo.html父级 位于boo.example.com的helloworld.php 如果您在v9.62上测试的Opera以外的浏览器中测试上述代码,它就像我在Safari、Firefox和Chrome中测试过的魅力一样。在Opera中,它不起作用,并

你需要Opera 9.62来了解这一切。。。因为当我使用Ajax进行跨子域JavaScript调用时,这是唯一一个行为奇怪的浏览器。请考虑以下三个简单文件,并将它们放在适当的域中。 foo.example.com上boo.html iframe的foo.html父级

位于boo.example.com的helloworld.php

如果您在v9.62上测试的Opera以外的浏览器中测试上述代码,它就像我在Safari、Firefox和Chrome中测试过的魅力一样。在Opera中,它不起作用,并抛出一个带有安全冲突消息的错误。有人知道是怎么回事吗? 我已经找到了这个问题的解决方案,稍后我会在这里发布。我也想看看你的解决方案,但我也想了解更多关于这个问题的信息——有人能解释一下吗


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

认为Opera版本不支持document.domain

我知道9之前的Opera版本都不支持它,所以我猜9.62也不支持它

尝试将domain设置为boo.example.com,看看是否有效,以防9.62支持它


据介绍,它确实很好地支持XMLHttpRequest,而且由于它遵循W3C规范,var request=new XMLHttpRequest应该可以正常工作。

我打算建议您实现CORS,但这似乎还没有实现:

让我向您展示在Opera 9.62中工作的代码

<!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() { 
        setTimeout(function() {
            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('');
            }
        }, 1);
    }

</script> 
<head>
<body>
</body>
</html>
<!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>
<?php
    echo 'Hello World!';
?>
<!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() { 
        setTimeout(function() {
            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('');
            }
        }, 1);
    }

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