Файл: src/javascript/features/ajax/-lib.js
Строк: 43
<?php
// https://github.com/yanatan16/nanoajax/blob/master/index.js
exports.init = function (params, callback) {
if (typeof params === 'string') params = {url: params};
var headers = params.headers || {},
body = params.body,
method = params.method || (body ? 'POST' : 'GET'),
withCredentials = params.withCredentials || false;
var req = getRequest();
req.onreadystatechange = function () {
if (req.readyState === 4) callback(req.status, req.responseText, req);
};
if (body) {
setDefault(headers, 'X-Requested-With', 'XMLHttpRequest');
setDefault(headers, 'Content-Type', 'application/x-www-form-urlencoded');
}
req.open(method, params.url, true);
// has no effect in IE
// has no effect for same-origin requests
// has no effect in CORS if user has disabled 3rd party cookies
req.withCredentials = withCredentials;
for (var field in headers) {
if (headers.hasOwnProperty(field)) req.setRequestHeader(field, headers[field]);
}
req.send(body);
};
function getRequest() {
if (global.XMLHttpRequest) {
return new global.XMLHttpRequest();
}
try {
return new global.ActiveXObject('MSXML2.XMLHTTP.3.0');
} catch (e) {}
throw new Error('no xmlhttp request able to be created');
}
function setDefault(obj, key, value) {
obj[key] = obj[key] || value;
}
?>