SIGN IN SIGN UP
const MIME_TYPES = {
2024-01-06 11:51:57 +01:00
json: "application/json",
html: "text/html",
};
2013-10-24 20:25:52 +02:00
2024-01-06 15:50:23 +01:00
function ajax(options) {
applyDefaults(options);
serializeData(options);
2013-10-24 20:25:52 +02:00
const xhr = new XMLHttpRequest();
xhr.open(options.type, options.url, options.async);
2013-10-24 20:25:52 +02:00
applyCallbacks(xhr, options);
applyHeaders(xhr, options);
2013-10-24 20:25:52 +02:00
xhr.send(options.data);
2013-10-24 20:25:52 +02:00
if (options.async) {
2024-01-06 11:51:57 +01:00
return { abort: abort.bind(undefined, xhr) };
} else {
return parseResponse(xhr, options);
}
2013-10-24 20:25:52 +02:00
2024-01-06 16:39:10 +01:00
function applyDefaults(options) {
for (var key in ajax.defaults) {
if (options[key] == null) {
options[key] = ajax.defaults[key];
}
2024-01-06 11:51:57 +01:00
}
}
2024-01-06 16:39:10 +01:00
function serializeData(options) {
if (!options.data) {
return;
}
2024-01-06 16:39:10 +01:00
if (options.type === "GET") {
options.url += "?" + serializeParams(options.data);
options.data = null;
} else {
options.data = serializeParams(options.data);
}
}
2024-01-06 16:39:10 +01:00
function serializeParams(params) {
return Object.entries(params)
.map(
([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIComponent(value)}`,
)
.join("&");
2024-01-06 11:51:57 +01:00
}
2024-01-06 16:39:10 +01:00
function applyCallbacks(xhr, options) {
if (!options.async) {
return;
}
2024-01-06 16:39:10 +01:00
xhr.timer = setTimeout(
onTimeout.bind(undefined, xhr, options),
options.timeout * 1000,
);
if (options.progress) {
xhr.onprogress = options.progress;
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
clearTimeout(xhr.timer);
onComplete(xhr, options);
}
};
2024-01-06 11:51:57 +01:00
}
2024-01-06 16:39:10 +01:00
function applyHeaders(xhr, options) {
if (!options.headers) {
options.headers = {};
}
2024-01-06 16:39:10 +01:00
if (options.contentType) {
options.headers["Content-Type"] = options.contentType;
}
2024-01-06 16:39:10 +01:00
if (
!options.headers["Content-Type"] &&
options.data &&
options.type !== "GET"
) {
options.headers["Content-Type"] = "application/x-www-form-urlencoded";
}
if (options.dataType) {
options.headers["Accept"] =
MIME_TYPES[options.dataType] || options.dataType;
}
2024-01-06 16:39:10 +01:00
for (var key in options.headers) {
var value = options.headers[key];
xhr.setRequestHeader(key, value);
}
}
2024-01-06 16:39:10 +01:00
function onComplete(xhr, options) {
if (200 <= xhr.status && xhr.status < 300) {
let response;
if ((response = parseResponse(xhr, options)) != null) {
onSuccess(response, xhr, options);
} else {
onError("invalid", xhr, options);
}
} else {
2024-01-06 16:39:10 +01:00
onError("error", xhr, options);
}
}
2024-01-06 16:39:10 +01:00
function onSuccess(response, xhr, options) {
if (options.success != null) {
options.success.call(options.context, response, xhr, options);
}
}
2024-01-06 16:39:10 +01:00
function onError(type, xhr, options) {
if (options.error != null) {
options.error.call(options.context, type, xhr, options);
}
}
2024-01-06 16:39:10 +01:00
function onTimeout(xhr, options) {
xhr.abort();
onError("timeout", xhr, options);
}
2024-01-06 16:39:10 +01:00
function abort(xhr) {
clearTimeout(xhr.timer);
xhr.onreadystatechange = null;
xhr.abort();
}
2024-01-06 16:39:10 +01:00
function parseResponse(xhr, options) {
if (options.dataType === "json") {
return parseJSON(xhr.responseText);
} else {
return xhr.responseText;
}
}
2024-01-06 16:39:10 +01:00
function parseJSON(json) {
try {
return JSON.parse(json);
} catch (error) {}
}
}
ajax.defaults = {
async: true,
dataType: "json",
timeout: 30,
type: "GET",
// contentType
// context
// data
// error
// headers
// progress
// success
// url
};