-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
110 lines (96 loc) · 3.73 KB
/
index.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Best place to find information on XHR features is:
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
var reqfields = [
'responseType', 'withCredentials', 'timeout', 'onprogress'
]
// Simple and small ajax function
// Takes a parameters object and a callback function
// Parameters:
// - url: string, required
// - headers: object of `{header_name: header_value, ...}`
// - body:
// + string (sets content type to 'application/x-www-form-urlencoded' if not set in headers)
// + FormData (doesn't set content type so that browser will set as appropriate)
// - method: 'GET', 'POST', etc. Defaults to 'GET' or 'POST' based on body
// - cors: If your using cross-origin, you will need this true for IE8-9
//
// The following parameters are passed onto the xhr object.
// IMPORTANT NOTE: The caller is responsible for compatibility checking.
// - responseType: string, various compatability, see xhr docs for enum options
// - withCredentials: boolean, IE10+, CORS only
// - timeout: long, ms timeout, IE8+
// - onprogress: callback, IE10+
//
// Callback function prototype:
// - statusCode from request
// + Possibly null or 0 (i.e. falsy) if an error occurs
// - response
// + if responseType set and supported by browser, this is an object of some type (see docs)
// + otherwise if request completed, this is the string text of the response
// + if request is aborted, this is "Abort"
// + if request times out, this is "Timeout"
// + if request errors before completing (probably a CORS issue), this is "Error"
// - request object
//
// Returns the request object. So you can call .abort() or other methods
//
// DEPRECATIONS:
// - Passing a string instead of the params object has been removed!
//
exports.ajax = function (params, callback) {
// Any variable used more than once is var'd here because
// minification will munge the variables whereas it can't munge
// the object access.
var headers = params.headers || {}
, body = params.body
, method = params.method || (body ? 'POST' : 'GET')
, called = false
var req = getRequest(params.cors)
function cb(statusCode, responseText) {
return function () {
if (!called) {
callback(req.status === undefined ? statusCode : req.status,
req.status === 0 ? "Error" : (req.response || req.responseText || responseText),
req)
called = true
}
}
}
req.open(method, params.url, true)
var success = req.onload = cb(200)
req.onreadystatechange = function () {
if (req.readyState === 4) success()
}
req.onerror = cb(null, 'Error')
req.ontimeout = cb(null, 'Timeout')
req.onabort = cb(null, 'Abort')
if (body) {
setDefault(headers, 'X-Requested-With', 'XMLHttpRequest')
if (!global.FormData || !(body instanceof global.FormData)) {
setDefault(headers, 'Content-Type', 'application/x-www-form-urlencoded')
}
}
for (var i = 0, len = reqfields.length, field; i < len; i++) {
field = reqfields[i]
if (params[field] !== undefined)
req[field] = params[field]
}
for (var field in headers)
req.setRequestHeader(field, headers[field])
req.send(body)
return req
}
function getRequest(cors) {
// XDomainRequest is only way to do CORS in IE 8 and 9
// But XDomainRequest isn't standards-compatible
// Notably, it doesn't allow cookies to be sent or set by servers
// IE 10+ is standards-compatible in its XMLHttpRequest
// but IE 10 can still have an XDomainRequest object, so we don't want to use it
if (cors && global.XDomainRequest && !/MSIE 1/.test(navigator.userAgent))
return new XDomainRequest
if (global.XMLHttpRequest)
return new XMLHttpRequest
}
function setDefault(obj, key, value) {
obj[key] = obj[key] || value
}