代码之家  ›  专栏  ›  技术社区  ›  Josef Pfleger

使用混合iframe代理/xsl/jsonp概念用Javascript加载跨域XML?

  •  3
  • Josef Pfleger  · 技术社区  · 16 年前

    www.foo.com http://feeds.foo.com/feed.xml 使用Javascript。我们显然会使用 Access-Control 但对于不支持它的浏览器,我们将以下内容视为一种退路:

    在…上 www.foo.com document.domain ,提供回调函数并将提要加载到(隐藏) iframe

    document.domain = 'foo.com';
    function receive_data(data) {
     // process data
    };
    
    var proxy = document.createElement('iframe');
    proxy.src = 'http://feeds.foo.com/feed.xml';
    document.body.appendChild(proxy);
    

    在…上 feeds.foo.com feed.xml 并使用它将提要转换为html文档,该文档还设置 并以json形式调用其父级中的回调函数:

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
     <xsl:template match="ROOT">
      <html><body>
       <script type="text/javascript">
        document.domain = 'foo.com';
        parent.receive_data([<xsl:apply-templates/>]);
       </script>
      </body></html>
     </xsl:template>
     <!-- templates that transform data into json objects go here -->
    </xsl:stylesheet>
    


    评论

    • 很好。
    • 我们只需要很少或根本不需要改变
    • 我们知道(但不感兴趣)服务器端代理解决方案
    • 更新: wrote about it
    2 回复  |  直到 16 年前
        1
  •  1
  •   Justin Grant    16 年前

    如果您对这两个域都有控制权,可以尝试使用跨域脚本库,如 EasyXDM ,它包装了跨浏览器的怪癖,并提供了一个易于使用的API,用于使用该浏览器的最佳可用机制(例如。 postMessage 如果可用,其他机制(如果不可用)。

    另一个警告:这里有安全隐患——确保您信任其他域的脚本!

        2
  •  2
  •   Juraj Blahunka    16 年前

    您可以使用yahoo API( YQL ).. 只需指定url、格式和回调

    这是一种服务器端解决方案,但不是在您的服务器上:)