解析XML的iframe [英] parse xml in an iframe
问题描述
我希望能够获得来自爵士的RSS源。我必须配置两个服务器使用相同的域(但不同的子域 - 例如static.benanderson.us和tech.benanderson.us)的能力。我希望我可以使用document.domain属性,以绕过XSS问题。这里有一个片段 http://static.benanderson.us/example.js (未实际生活)
I want to be able access an rss feed from js. I have the ability to configure both servers to use the same domain (but different subdomains - eg static.benanderson.us and tech.benanderson.us). I was hoping I could use the document.domain property to get around the xss issue. Here's a snippet http://static.benanderson.us/example.js (not actually live):
document.domain = 'benanderson.us';
new Ajax.Request('http://tech.benanderson.us/feeds/posts/default', { \\error
然而,这是行不通的。我无法找出是否document.domain的作品XHR请求,所以我袋装的,并切换到一个iframe解决方案,因为我已经做了,在过去类似的事情。
However, that doesn't work. I couldn't find out if document.domain works for xhr requests, so I bagged that and switched to an iframe solution because I've done something similar in the past.
$('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查看this.contentDocument,但它是一个错误的getElementsByTagName权限被拒绝......的字样。
The weird thing is that I can view this.contentDocument in firebug, however it's the getElementsByTagName that errors with a "permission denied..." message.
如何让两个方案工作的任何想法将真棒。我知道我可以做一个代理 - 这不是我感兴趣的
Any thoughts on how to get either of these solutions to work would be awesome. I know I could do a proxy - that's not what I'm interested in.
推荐答案
显然是没有办法做的正是这一点。 Howver,我能够拿出一个体面的解决办法。该RSS XML是从博客(tech.benanderson.us)的到来。所以我增加了一个javascript函数有可能使XHR的RSS。然后此javascript设置它的document.domain到benanderson.us,使一个回调。综上所述:
apparently there is no way to do exactly this. Howver, I was able to come up with a decent solution. The rss xml is coming from blogger (tech.benanderson.us). So I added a javascript function there that could make the xhr to the rss. Then this javascript sets it's document.domain to benanderson.us and makes a callback. To sum up:
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 < /一>:
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);
}
});
这篇关于解析XML的iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!