Can't get feed in Windows 8 app

2019-05-31 19:37发布

问题:

I am creating blog reader app for Windows 8 by using RSS feeds. Part of code:

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);
            }
        }
    });
}

So, my app can't read feed from FeedBurner. I get this error

Can't load http://feeds.feedburner.com/~d/styles/itemcontent.css. An app can’t load remote web content in the local context.

I've tried http://feeds.feedburner.com/CssTricks?format=xml and http://feeds.feedburner.com/CssTricks?fmt=xml, but the same error.

EDIT: Full code: http://jsfiddle.net/8n67y/

回答1:

The error you're encountering isn't because you can't read from Feedburner. It's because somewhere in the content that you're attempting to load into the DOM is a reference to a CSS file on the web (itemcontent.css).

When you're operating in the local context, you cannot dynamically load script or CSS from the web, because that poses security risks in the local context.

See here:

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

and here:

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

for more information on the differences between local and web context, and the restrictions that apply to each.

For your particular case, I think what you should try is parsing the content further (you can set a breakpoint in the above code to examine the XML content being returned by the feed) to determine where the CSS file reference is being returned and either remove it programmatically, if it's in a consistent place, or find another means of eliminating the CSS reference, which appears to be what's causing the exception (based on the limited information above).



回答2:

What you are trying to do can be done with the below code, instead of WinJS.xhr use the xmlHTTPRequest.

The code below is part of the code I use um my RSS reader and it works pretty well in all situations, we can donwload pictures, text, links.. whatever you can find in the feed (http://feeds.feedburner.com/CssTricks) getting the thumbnails, and all worked fine.

Also I tested it with the following modification,

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;
}