1
// Copyright 2006 Google Inc.
6
// Downloads the given URL, calling the given callback with the results
7
// and the HTTP response code when the download is complete. The valid
10
// - post - use POST instead of GET
11
// - body - POST body (implies options.post)
12
// - contentType - POST content type (default x-www-form-urlencoded)
13
// - synchronous - block until request completes
14
// - username - HTTP Basic Authentication username
15
// - password - HTTP Basic Authentication password
17
function download(url, opt_callback, opt_options) {
18
var options = opt_options || {};
21
if (typeof ActiveXObject != 'undefined') {
22
request = new ActiveXObject('Microsoft.XMLHTTP');
23
} else if (window.XMLHttpRequest) {
24
request = new XMLHttpRequest();
26
throw new Error("XMLHttpRequest not supported");
30
request.onreadystatechange = function() {
31
if (request.readyState == 4) {
32
// Call the callback and clean up memory leaks
33
opt_callback.call(null, request.responseText, request.status);
34
request.onreadystatechange = returnFalse;
39
// You have to open the connection before setting the request headers
40
var requestType = "GET";
41
if (options.post || options.body) {
44
request.open(requestType, url, !options.synchronous);
46
if (requestType == "POST") {
47
var contentType = options.contentType ||
48
"application/x-www-form-urlencoded";
49
request.setRequestHeader("Content-Type", contentType);
51
if (options.username || options.password) {
52
request.send(options.body, options.username, options.password);
54
request.send(options.body);
57
if (options.synchronous && opt_callback) {
58
opt_callback.call(null, request.responseText, request.status);