~nchohan/+junk/mytools

« back to all changes in this revision

Viewing changes to sample_apps/tasks/static/javascript/debug/io.js

  • Committer: root
  • Date: 2010-11-03 07:43:57 UTC
  • Revision ID: root@appscale-image0-20101103074357-xea7ja3sor3x93oc
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2006 Google Inc.
 
2
// All Rights Reserved
 
3
//
 
4
// Author: Bret Taylor
 
5
 
 
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
 
8
// options are:
 
9
//
 
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
 
16
//
 
17
function download(url, opt_callback, opt_options) {
 
18
  var options = opt_options || {};
 
19
  var request;
 
20
 
 
21
  if (typeof ActiveXObject != 'undefined') {
 
22
    request = new ActiveXObject('Microsoft.XMLHTTP');
 
23
  } else if (window.XMLHttpRequest) {
 
24
    request = new XMLHttpRequest();
 
25
  } else {
 
26
    throw new Error("XMLHttpRequest not supported");
 
27
  }
 
28
 
 
29
  if (opt_callback) {
 
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;
 
35
      }
 
36
    }
 
37
  }
 
38
 
 
39
  // You have to open the connection before setting the request headers
 
40
  var requestType = "GET";
 
41
  if (options.post || options.body) {
 
42
    requestType = "POST";
 
43
  }
 
44
  request.open(requestType, url, !options.synchronous);
 
45
 
 
46
  if (requestType == "POST") {
 
47
    var contentType = options.contentType ||
 
48
                      "application/x-www-form-urlencoded";
 
49
    request.setRequestHeader("Content-Type", contentType);
 
50
  }
 
51
  if (options.username || options.password) {
 
52
    request.send(options.body, options.username, options.password);
 
53
  } else {
 
54
    request.send(options.body);
 
55
  }
 
56
 
 
57
  if (options.synchronous && opt_callback) {
 
58
    opt_callback.call(null, request.responseText, request.status);
 
59
  }
 
60
}