WebFM/src/core/static/js/ajax.js

62 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-02-08 02:07:13 +00:00
const listFilesAjax = async (hash) => {
const data = "empty=NULL";
doAjax("api/list-files/" + hash, data, "list-files");
}
const getFavesAjax = async () => {
const data = "empty=NULL";
2021-02-08 04:49:38 +00:00
doAjax("api/list-favorites", data, "favorites");
2021-02-08 02:07:13 +00:00
}
const loadFavoriteLink = async (id) => {
const data = "empty=NULL";
doAjax("api/load-favorite/" + id, data, "load-favorite");
}
const manageFavoritesAjax = async (action) => {
const data = "empty=NULL";
doAjax("api/manage-favorites/" + action, data, "manage-favorites");
}
const lockFoldersAjax = async () => {
const data = "empty=NULL";
doAjax("logout", data, "lock-folders");
}
2020-10-11 21:30:11 +00:00
const doAjax = (actionPath, data, action) => {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
if (this.responseText != null) { // this.responseXML if getting XML data
postAjaxController(JSON.parse(this.responseText), action);
2020-10-11 21:30:11 +00:00
} else {
let type = "danger"
let msg = "No content returned. Check the target path.";
data = '{"message": { "type": "' + type + '", "text": "' + text + '" } }'
postAjaxController(JSON.parse(data));
}
}
};
2021-02-08 02:07:13 +00:00
// xhttp.open("POST", formatURL(actionPath), true);
2020-10-11 21:30:11 +00:00
xhttp.open("POST", actionPath, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
2021-02-08 02:07:13 +00:00
if (action === "list-files") {
xhttp.setRequestHeader("Cache-Control", "no-cache, no-store");
xhttp.setRequestHeader("Pragma", "no-cache");
xhttp.setRequestHeader("Expires", "0");
}
2020-10-11 21:30:11 +00:00
// Force return to be JSON NOTE: Use application/xml to force XML
xhttp.overrideMimeType('application/json');
xhttp.send(data);
}
const fetchData = async (url) => {
2021-02-08 02:07:13 +00:00
let response = await fetch(url);
2020-10-11 21:30:11 +00:00
return await response.json();
}