代码之家  ›  专栏  ›  技术社区  ›  andersonbd1

在iframe中分析XML

  •  2
  • andersonbd1  · 技术社区  · 15 年前

    我希望能够从JS访问RSS源。我可以将两个服务器配置为使用相同的域(但使用不同的子域,如static.bendanderson.us和tech.bendanderson.us)。我希望可以使用document.domain属性来解决XSS问题。这是一个片段 http://static.benanderson.us/example.js (实际上不存在):

    document.domain = 'benanderson.us';
    new Ajax.Request('http://tech.benanderson.us/feeds/posts/default', { \\error
    

    但是,这不起作用。我不知道document.domain是否适用于xhr请求,所以我将其打包并切换到iframe解决方案,因为我以前做过类似的事情。

    $('my_iframe').src='http://tech.benanderson.us/feeds/posts/default';
    Event.observe($('my_iframe'), 'load', function() {
      try {
        log(this.contentDocument);  //this displays just fine
        var entries = this.contentDocument.getElementsByTagName('entry');  //error
    

    奇怪的是,我可以在Firebug中查看这个.contentDocument,但是GetElementsByTagname错误地显示了“权限被拒绝…”消息。

    任何关于如何让这些解决方案中的任何一个发挥作用的想法都是非常棒的。我知道我可以做代理-这不是我感兴趣的。

    5 回复  |  直到 13 年前
        1
  •  2
  •   Kev    15 年前

        2
  •  2
  •   andersonbd1    15 年前

    http://static.benanderson.us/example.js

    Event.observe(window, 'load', function() {
      document.domain = 'benanderson.us';
      $('my_iframe').src='http://tech.benanderson.us/2001/01/js.html';
    });
    function renderFeed(feedXml) {
      ...
    

    http://tech.benanderson.us/2001/01/js.html

    var url = 'http://tech.benanderson.us/feeds/posts/default';
    new Ajax.Request(url, {
      method: 'get',
      onSuccess: function(response) {
        document.domain = 'benanderson.us';
        top.renderFeed(response.responseXML);
      }
    });
    
        3
  •  0
  •   Arjan    15 年前

        4
  •  -1
  •   Coding With Style    15 年前

    <html>
    <frameset onload="document.domain='benanderson.us';window.frames['content_frame'].location=location.href.split('?request=')[1]">
    <frame name=content_frame onload="window.frames['content_frame'].document.domain='benanderson.us'">
    </frameset>
    </html>
    

    http://tech.benanderson.us/feeds/posts/default

        5
  •  -1
  •   Cem Kalyoncu    15 年前

        document.getElementById("internal").onload=function() {
            //wrap xml for easy usage
            var ret=parseXML(window.frames["internal"].document);
    
            //show what happened
            showResult(ret, [
                    new DataPair("başlık", "Eklenti Ekle"),
                    new DataPair("nesne" , "Eklenti")
            ]);
    
            //no error
            if(ret.findNodeValue("error")=="no") {
                //close
                eklentiEkleKapat();
                //Protection from refresh
                document.getElementById("internal").onload=function() {}
                window.frames["internal"].location.href="about:blank";
                //activate attachments tab
                tab_eklentiler.activate(true);
            }
        }
    
    
        //I actually use a form to post here
        document.getElementById("internal").location.href="target.php";