23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

XmlHttpRequest<br />

As we’ve seen a number of times already in this book, transferring data to and from web services with<br />

XmlHttpRequest is a common activity for Windows Store apps, especially those written in JavaScript for<br />

which handling XML and/or JSON is simple and straightforward. This is especially true when using the<br />

WinJS.xhr wrapper that turns the whole process into a simple promise.<br />

To build on what we already covered in Chapter 3, in the section “Data from Services and WinJS.xhr,”<br />

there are a few other points to make where such requests are concerned, most of which come from the<br />

section in the documentation entitled Connecting to a web service.<br />

First, Downloading different types of content provides the details of the different content types<br />

supported by XHR for Windows Store apps. These are summarized here:<br />

Type Use responseText responseXML<br />

arraybuffer Binary content as an array of Int8 or Int64, or another integer or float undefined<br />

undefined<br />

type.<br />

Blob Binary content represented as a single entity. undefined undefined<br />

document An XML DOM object representing XML content (MIME type of undefined<br />

The XML content<br />

text/XML).<br />

json JSON strings. The JSON string undefined<br />

ms-stream Streaming data; see XmlHttpRequest enhancements. undefined undefined<br />

Text Text (the default). The text string undefined<br />

Second, know that XHR responses can be automatically cached, meaning that later requests to the<br />

same URI might return old data. To resend the request despite the cache, add an If-Modified-Since HTTP<br />

header as shown on How to ensure that WinJS.xhr resends requests.<br />

Along similar lines, you can wrap a WinJS.xhr operation in another promise to encapsulate<br />

automatic retries if there is an error in any given request. That is, build your retry logic around the core<br />

XHR operation, with the result stored in some variable. Then place that whole block of code within<br />

WinJS.Promise.wrap (or a new WinJS.Promise) and use that elsewhere in the app.<br />

In each XHR attempt, remember that you can also use WinJS.Promise.timeout in conjunction with<br />

WinJS.Xhr as described on Setting timeout values with WinJS.xhr., because WinJS.xhr doesn’t have a<br />

timeout notion directly. You can, of course, set a timeout in the raw request, but that would mean<br />

rebuilding everything that WinJS.xhr already does.<br />

Generally speaking, XHR headers are accessible to the app with the exception of cookies (the<br />

set-cookie and set-cookie2 headers), as these are filtered out by design for XHR done from a local<br />

context. They are not filtered for XHR from the web context, so if you need cookies, try acquiring them<br />

in a web context iframe and pass them to a local context using postMessage.<br />

642

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!