【问题标题】:whateverorigin run html code不管起源运行html代码
【发布时间】:2016-01-06 19:36:12
【问题描述】:

我正在尝试使用任何来源在iframe 中运行一个小型应用程序:

    <script>
    $.getJSON('http://whateverorigin.org/get?url=' +      
    encodeURIComponent('http://vend.giftcardservices.nl/saldo/3') + 
    '&callback=?', function(data){
    alert(data.contents);
    });
    </script>   

警报显示 html 已获取,但是当我尝试 document.write(data.contents) 时,什么也没有发生。如何让这段代码在浏览器中执行?

【问题讨论】:

    标签: javascript same-origin-policy


    【解决方案1】:

    您已经在使用 jQuery,don't use document.write。相反,使用

    $(document.body).append(data.content);
    

    如果您要将内容加载到特定元素中,则可以使用

    $('#selector').html(data.content);
    

    这是sn-p

    $.getJSON('http://whateverorigin.org/get?url=' +      
        encodeURIComponent('http://vend.giftcardservices.nl/saldo/3') + 
        '&callback=?', function(data){
        $(document.body).append(data.contents.replace('/static/js/saldo.js', 'http://vend.giftcardservices.nl/saldo/3/static/js/saldo.js'));
    });
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"&gt;&lt;/script&gt;

    【讨论】:

    • 然而,这确实带来了小部件的功能不再起作用的效果 - 找不到 URL。有什么解决方案的想法吗?
    • 嗯,然后使用 iframe,它们允许 CORS。
    • 啊,好的。还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多