-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathio.js
More file actions
60 lines (54 loc) · 1.86 KB
/
Copy pathio.js
File metadata and controls
60 lines (54 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2006 Google Inc.
// All Rights Reserved
//
// Author: Bret Taylor
// Downloads the given URL, calling the given callback with the results
// and the HTTP response code when the download is complete. The valid
// options are:
//
// - post - use POST instead of GET
// - body - POST body (implies options.post)
// - contentType - POST content type (default x-www-form-urlencoded)
// - synchronous - block until request completes
// - username - HTTP Basic Authentication username
// - password - HTTP Basic Authentication password
//
function download(url, opt_callback, opt_options) {
var options = opt_options || {};
var request;
if (typeof ActiveXObject != 'undefined') {
request = new ActiveXObject('Microsoft.XMLHTTP');
} else if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
throw new Error("XMLHttpRequest not supported");
}
if (opt_callback) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
// Call the callback and clean up memory leaks
opt_callback.call(null, request.responseText, request.status);
request.onreadystatechange = returnFalse;
}
}
}
// You have to open the connection before setting the request headers
var requestType = "GET";
if (options.post || options.body) {
requestType = "POST";
}
request.open(requestType, url, !options.synchronous);
if (requestType == "POST") {
var contentType = options.contentType ||
"application/x-www-form-urlencoded";
request.setRequestHeader("Content-Type", contentType);
}
if (options.username || options.password) {
request.send(options.body, options.username, options.password);
} else {
request.send(options.body);
}
if (options.synchronous && opt_callback) {
opt_callback.call(null, request.responseText, request.status);
}
}