注重与跨域Ajax在Opera [英] Focus with Cross-domain Ajax in Opera

查看:81
本文介绍了注重与跨域Ajax在Opera的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将需要歌剧9.62,看看这是怎么一回事......因为这是一个行为奇怪,当我做跨子域的JavaScript调用(使用Ajax参与)的浏览器。请考虑以下三个简单的文件,并将它们在适当的领域。

You'll need Opera 9.62 to see what this is all about... Because that is the only browser that behaves strange when I do cross-sub-domain JavaScript calls (with Ajax involved). Please consider the following three simple files and place them at appropriate domains.

foo.html (boo.html IFRAME的母公司)在 foo.example.com

foo.html (parent of boo.html iframe) at foo.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>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

boo.html (iframe of foo.html) at 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>

helloworld.php boo.example.com

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

如果您测试的浏览器比Opera等符合上述要求code(上v9.62测试),它就像一个魅力(我在Safari浏览器,火狐,Chrome已经测试)。在Opera,它不工作,并与安全冲突的消息将引发错误。有谁知道问题出在哪儿? 我已经找到了一个解决问题的办法,我会有点后张贴在这里(我也想看看您的解决方案),但我想更多地了解这个问题以及 - ?有谁能够解释

If you test the above-stated code in browsers other than Opera (tested on v9.62), it works like a charm (I have tested in Safari, Firefox, Chrome). In Opera, it does not work and an error with security violation message is thrown. Does anybody know what the matter is? I have found out a solution to the problem and I will post it here a bit later (I'd also like to see your solutions), but I'd like to learn more about the issue as well - can anybody explain it?

NB :我已经设置了所有的文件在我自己的服务器上,这样你就可以检查出来的

NB: I have set up all the files at my own server, so you can check it out here

更新:我只是测试它在最新的歌剧10.63 和它的不存在这样的问题。所以,你一定会需要为使用Opera v9.62来观察问题

UPDATE: I just tested it on the newest Opera 10.63 and it does not have such a problem. So you'll definitely need to use Opera v9.62 to observe the problem.

推荐答案

一些老的歌剧版本了,使得设置document.domain的影响的安全上下文XMLHtt prequest一个已知的bug。因此,设置document.domain的剧本后,不再允许从它其实是从服务器加载内容。

Some older Opera versions had a known bug that made setting document.domain affect the security context for XMLHttpRequest. Hence, after setting document.domain the script is no longer allowed to load contents from the server it actually came from.

推荐的解决方案是简单地升级到不受该漏洞的一个版本,但是如果你绝对需要支持9.6x,你可以很容易地检测出异常,并改回使用的postMessage()跨域通信。 (在这样的旧版本,则需要调用document.postMessage() - 在新版本中,它是window.postMessage(),但旧版本的HTML5规范它最初是在文档中定义的)

The recommended solution is to simply upgrade to a version not affected by the bug, however if you absolutely need to support 9.6x you can easily detect the exception and fall back to using postMessage() for cross-domain communication. (In such an old version, you will need to call document.postMessage() - in newer versions it's window.postMessage() but it older versions of the HTML5 specification it was originally defined on document.)

这篇关于注重与跨域Ajax在Opera的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆