【问题标题】:Can't get feed in Windows 8 app无法在 Windows 8 应用程序中获取源
【发布时间】:2012-11-25 18:45:14
【问题描述】:

我正在使用 RSS 源创建适用于 Windows 8 的博客阅读器应用程序。部分代码:

function downloadBlogFeed() {
    WinJS.xhr({ url: "http://feeds.feedburner.com/CssTricks" }).then(function (rss) {
        var items = rss.responseXML.querySelectorAll("item");

        for (var n = 0; n < items.length; n++) {
            var article = {};
            article.title = items[n].querySelector("title").textContent;
            var thumbs = items[n].querySelectorAll("thumbnail");
            if (thumbs.length > 1) {
                article.thumbnail = thumbs[1].attributes.getNamedItem("url").textContent;
                article.content = items[n].textContent;
                articlesList.push(article);
            }
        }
    });
}

所以,我的应用无法从 FeedBurner 读取提要。我收到此错误

无法加载http://feeds.feedburner.com/~d/styles/itemcontent.css。应用无法在本地上下文中加载远程 Web 内容。

我试过http://feeds.feedburner.com/CssTricks?format=xmlhttp://feeds.feedburner.com/CssTricks?fmt=xml,但同样的错误。

编辑:完整代码:http://jsfiddle.net/8n67y/

【问题讨论】:

    标签: javascript rss windows-8 winjs feedburner


    【解决方案1】:

    您遇到的错误不是因为您无法从 Feedburner 读取数据。这是因为您尝试加载到 DOM 的内容中的某处是对 Web 上的 CSS 文件 (itemcontent.css) 的引用。

    当您在本地环境中操作时,您不能从 Web 动态加载脚本或 CSS,因为这会在本地环境中带来安全风险。

    看这里:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh465380.aspx

    这里:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh465373.aspx

    有关本地环境和网络环境之间差异的更多信息,以及适用于每种环境的限制。

    对于您的特定情况,我认为您应该尝试进一步解析内容(您可以在上述代码中设置断点以检查提要返回的 XML 内容)以确定 CSS 文件引用的返回位置并以编程方式将其删除(如果它位于一致的位置),或者找到另一种消除 CSS 引用的方法,这似乎是导致异常的原因(基于上面的有限信息)。

    【讨论】:

    • 这个问题有解决办法吗?
    • A 解决方案,如上所述,以删除外部 CSS 链接的方式解析提要内容。如果没有看到更多关于articlesList 的内容,很难提供更具体的解决方案。
    【解决方案2】:

    您可以使用以下代码来完成您想要做的事情,而不是 WinJS.xhr 使用 xmlHTTPRequest。

    下面的代码是我使用 RSS 阅读器的代码的一部分,它在所有情况下都运行良好,我们可以下载图片、文本、链接.. 无论你在提要 (http://feeds.feedburner.com/CssTricks) 中找到什么缩略图,一切正常。

    我还用以下修改对其进行了测试,

    function connectToURL() {
        var url = "";
    
        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null) {
             return;
        }   
    
        xmlHttp.onreadystatechange = stateChanged;
        xmlHttp.open("GET", url,true);
        xmlHttp.send(null);
    }
    
    // your job will actually start on this one...
    function stateChanged() {
            if(xmlHttp != null )
                if (xmlHttp[item.key].readyState == 4 ) {
                    try {
                        var xmlDoc = xmlHttp.responseXML.documentElement.getElementsByTagName("TAGYOUWANTTOGET");
                        for (var i = 0; i < xmlDoc.length; i++) {
                           xmlDoc[i].getElementsByTagName("TAG")[0].childNodes[0].nodeValue
                        }
                    } catch (e) {
                       //work on the exception
                    }
                }
            }     
    }
    
    function GetXmlHttpObject() {
        var xmlHttp = null;
        try {
            xmlHttp = new XMLHttpRequest();
        }
        catch(e) {
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
    
        return xmlHttp;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 2015-05-28
      • 2013-10-06
      相关资源
      最近更新 更多