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