Loading cross domain XML with Javascript using a hybrid iframe-proxyxsljsonp concept? - Stack Overflow

On our site www.foo we want to download and use .xml with Javascript. We'll obviously use Access-C

On our site www.foo we want to download and use .xml with Javascript. We'll obviously use Access-Control but for browsers that don't support it we are considering the following as a fallback:

On www.foo, we set document.domain, provide a callback function and load the feed into a (hidden) iframe:

document.domain = 'foo';
function receive_data(data) {
 // process data
};

var proxy = document.createElement('iframe');
proxy.src = '.xml';
document.body.appendChild(proxy);

On feeds.foo, add an XSL to feed.xml and use it to transform the feed into an html document that also sets document.domain and calls the callback function in its parent with the feed data as json:

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

Is there a better way to load XML from feeds.foo and what are the ramifications of this iframe-proxy/xslt/jsonp trick? (..and in what cases will it fail?)


Remarks

  • This does not work in Safari & Chrome but since both support Access-Control it's fine.
  • We want little or no change to feeds.foo
  • We are aware of (but not interested in) server-side proxy solutions
  • update: wrote about it

On our site www.foo. we want to download and use http://feeds.foo./feed.xml with Javascript. We'll obviously use Access-Control but for browsers that don't support it we are considering the following as a fallback:

On www.foo., we set document.domain, provide a callback function and load the feed into a (hidden) iframe:

document.domain = 'foo.';
function receive_data(data) {
 // process data
};

var proxy = document.createElement('iframe');
proxy.src = 'http://feeds.foo./feed.xml';
document.body.appendChild(proxy);

On feeds.foo., add an XSL to feed.xml and use it to transform the feed into an html document that also sets document.domain and calls the callback function in its parent with the feed data as json:

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

Is there a better way to load XML from feeds.foo. and what are the ramifications of this iframe-proxy/xslt/jsonp trick? (..and in what cases will it fail?)


Remarks

  • This does not work in Safari & Chrome but since both support Access-Control it's fine.
  • We want little or no change to feeds.foo.
  • We are aware of (but not interested in) server-side proxy solutions
  • update: wrote about it
Share edited Feb 6, 2010 at 13:14 Josef Pfleger asked Dec 9, 2009 at 17:11 Josef PflegerJosef Pfleger 74.6k16 gold badges98 silver badges100 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You can use yahoo apis (YQL).. Just specify url, format and callback

  • Cross domain requests with jQuery by James Padolsey
  • jQuery plugin

It's kind of server-side solution, however not on your server :)

If you have control over both domains, you can try a cross-domain scripting library like EasyXDM, which wraps cross-browser quirks and provides an easy-to-use API for municating in client script between different domains using the best available mechanism for that browser (e.g. postMessage if available, other mechanisms if not).

Caveat: you need to have control over both domains in order to make it work (where "control" means you can place static files on both of them). But you don't need any server-side code changes.

Another Caveat: there are security implications here-- make sure you trust the other domain's script!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744930654a4601715.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信