Connecting to web services (Windows Store apps using JavaScript and HTML)

Language: JavaScript and HTML | VB/C#/C++ and XAML
6 out of 7 rated this helpful - Rate this topic

The topics in this section introduce a Windows Library for JavaScript wrapper for XMLHttpRequest called WinJS.xhr. WinJS.xhr provides an easy way to access web content in your Windows Store app built for Windows using JavaScript. You can use WinJS.xhr to make HTTP or HTTPS requests to download or upload files of any format.

Unlike XMLHttpRequest, which allows you to specify whether the request should be made asynchronously, WinJS.xhr is always asynchronous. WinJS.xhr returns a WinJS.Promise, so that you can handle completed requests, errors, and in-progress requests by using the then or done methods.

Warning  It is now possible to use XMLHttpRequest to transfer extremely large objects, such as Blob objects and FormData objects, which might take a long time to complete. Because Windows Store apps using JavaScript can be terminated at any time, you should consider using the file upload APIs in the Windows Runtime API for these operations. For more info about uploading content, see Quickstart: Uploading a file.

In this section

TopicDescription

Quickstart:Downloading a file with WinJS.xhr

WinJS.xhr provides an easy way to download web content in your Windows Store app using JavaScript. This topic shows how to download a file using WinJS.xhr, handle any errors, and report on the progress of the download. It also shows how to download different types of content.

How to upload binary data with WinJS.xhr

You can upload and download binary data when you use WinJS.xhr (which wraps XMLHttpRequest). This example shows how to upload a bitmap from your Pictures Library. The Windows Runtime method that is used to open the bitmap file returns an IRandomAccessStream, so you must use MSApp.createBlobFromRandomAccessStream to convert it to a Blob.

Warning  You can now use XMLHttpRequest to upload or download objects that are larger than a few MB, such as Blob objects and FormData objects, which may take a long time to complete. Because Windows Store apps using JavaScript can be terminated at any time, you should consider using the Windows Runtime background transfer APIs for these operations. For more information about uploading and downloading content, see Quickstart: Uploading a file and Quickstart: Downloading a file. For a general discussion of background transfer, see Transferring data in the background.

How to ensure that WinJS.xhr resends requests

When you request Web resources by using WinJS.xhr, the response might be cached, which means that later requests will return the version of the resource that already exists on the client computer rather than resending the request. However, you can add a HTTP header that ensures that the request is sent again, even if it has already been cached.

Setting timeout values

When you use XMLHttpRequest, you can set time-out values directly, but you cannot do this when you use WinJS.xhr. However, there is a way to set time-outs on WinJS.Promise objects. By calling WinJS.Promise.timeout, you ensure that the request is canceled if it has not completed within the specified time.

 

 

 

Build date: 10/26/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.