A mashup is a web application that uses data from two or more sources to create something new. This example shows you how to use XHR to retrieve and display a remote rss feed.
The WinJS.xhr function uses XHR to retrieve data from a specified URL. The WinJS.xhr is asynchronous and returns aPromise for the requested data. To obtain the RSS feed, you specify a fulfilled handler for this Promise. (You can also specify an error handler and a progress handler.)
You can call the WinJS.xhr function from any page in your package (any page in the local context).
Note Note: The target URL for an XHR request is not restricted by the app's ApplicationContentUriRules (which are listed in the app's package manifest).
This example uses WinJS.xhr to access the rss.msnbc.msn.com RSS feed. It passes two callback functions to the Promise:
- xhrParseXml: The fulfilled handler. This function is called if the XHR request was successful.
- xhrError: The error handler. This function is called if the request was not successful.
You'll define the xhrParseXml and xhrError methods in the next step.
If the WinJS.xhr request is successful, the code in the last example calls the xhrParseXml. The xhrParseXml function iterates through the RSS items and generates a link to each one. It displays the link in a div on the main page, xhrOutput.
The next example shows the HTML declaration for the output area.
There's always a chance that you won't be able to access the remote data, so provide a way to handle errors processing your XHR request. This example defines a simple error handler.
For a downloadable sample that contains more features, see the Integrating content and controls from web services sample.