@wcardinal/wcardinal-geditor
Version:
WebGL-based graphic editor, tester and viewer for supervisory systems
74 lines • 2.27 kB
JavaScript
/*
* Copyright (C) 2021 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
export var UtilAjax = {
send: function (method, settings) {
var isCompleted = false;
var makeOnError = function (reason) {
return function () {
if (!isCompleted) {
isCompleted = true;
settings.error(reason, xhr);
}
};
};
var xhr = new XMLHttpRequest();
xhr.open(method, settings.url, true);
xhr.onload = function () {
if (!isCompleted) {
isCompleted = true;
var status_1 = xhr.status;
if ((200 <= status_1 && status_1 < 300) ||
status_1 === 0 ||
status_1 === 304 ||
status_1 === 1223) {
settings.success(xhr.responseText, xhr);
}
else {
settings.error(xhr.statusText, xhr);
}
}
};
var onError = makeOnError("error");
xhr.onerror = onError;
xhr.onabort = makeOnError("abort");
// Headers
var headers = settings.headers;
if (headers != null) {
for (var name_1 in headers) {
var header = headers[name_1];
if (header != null) {
xhr.setRequestHeader(name_1, header);
}
}
}
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
// Timeout
var timeout = settings.timeout;
if (timeout != null) {
xhr.timeout = timeout;
xhr.ontimeout = makeOnError("timeout");
if (0 < timeout) {
// For browsers which do not support the `timeout`
setTimeout(function () {
xhr.abort();
}, timeout);
}
}
// Send
try {
var data = settings.data;
if (data != null) {
xhr.send(data);
}
else {
xhr.send();
}
}
catch (_a) {
onError();
}
}
};
//# sourceMappingURL=util-ajax.js.map