Performing Ajax Calls with the xhr Function

The WinJS xhr() function is a thin wrapper around the browser XMLHttpRequest object. Unlike working with the XMLHttpRequest object, the xhr() function returns a promise. You use the xhr() function whenever you want to make an Ajax request.

Listings 2.15 and 2.16 contain the code for making a simple Ajax request. The home page of the Microsoft site is retrieved and the list of all of the links extracted from this page is displayed (see Figure 2.9).

Image

FIGURE 2.9 Requesting and displaying links from the Microsoft homepage

LISTING 2.15 Making a Simple Ajax Request (xhr\xhr.html)

Get Windows® 8.1 Apps with HTML5 and JavaScript Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.