X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Futils%2Fajax.js;h=7d520ec8cc6a5e78b0387b61a4b9a15139b0a3dc;hb=d9a7a1e40254bda6e545514596a7363048c084f9;hp=065f382517ddf44965c9308a389ef6c6d2802496;hpb=625022fdcf750f0aff8fcd699f7e9b89730e1d10;p=vchess.git diff --git a/client/src/utils/ajax.js b/client/src/utils/ajax.js index 065f3825..7d520ec8 100644 --- a/client/src/utils/ajax.js +++ b/client/src/utils/ajax.js @@ -1,54 +1,54 @@ +import params from "../parameters"; //for server URL +import { store } from "../store"; //for translations + // From JSON (encoded string values!) to "arg1=...&arg2=..." -function toQueryString(data) -{ - let data_str = ""; - Object.keys(data).forEach(k => { - data_str += k + "=" + encodeURIComponent(data[k]) + "&"; - }); - return data_str.slice(0, -1); //remove last "&" +function toQueryString(data) { + let data_str = ""; + Object.keys(data).forEach(k => { + data_str += k + "=" + encodeURIComponent(data[k]) + "&"; + }); + return data_str.slice(0, -1); //remove last "&" } -// data, error: optional -export function ajax(url, method, data, success, error) -{ - let xhr = new XMLHttpRequest(); - if (typeof(data) === "function") //no data - { - error = success; - success = data; - data = {}; - } - if (!error) - error = errmsg => { alert(errmsg); }; - - xhr.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) - { - try { - let res_json = JSON.parse(xhr.responseText); - if (!res_json.errmsg) - success(res_json); - else - error(res_json.errmsg); - } catch (e) { - // Plain text (e.g. for rules retrieval) - success(xhr.responseText); - } - } - }; - - if (["GET","DELETE"].includes(method) && !!data) - { - // Append query params to URL - url += "/?" + toQueryString(data); - } - xhr.open(method, url, true); - xhr.setRequestHeader('X-Requested-With', "XMLHttpRequest"); - if (["POST","PUT"].includes(method)) - { - xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); - xhr.send(JSON.stringify(data)); - } - else - xhr.send(); +// TODO: use this syntax https://stackoverflow.com/a/29823632 ? +// data, success, error: optional +export function ajax(url, method, options) { + options = options || {}; + const data = options.data || {}; + // By default, do nothing on success and print errors: + if (!options.success) + options.success = () => {}; + if (!options.error) { + options.error = (errmsg) => { + alert(store.state.tr[errmsg] || errmsg); + }; + } + if (["GET", "DELETE"].includes(method) && !!data) + // Append query params to URL + url += "/?" + toQueryString(data); + const headers = { + "Content-Type": "application/json;charset=UTF-8", + "X-Requested-With": "XMLHttpRequest" + }; + let fetchOptions = { + method: method, + headers: headers, + }; + if (["POST", "PUT"].includes(method)) + fetchOptions["body"] = JSON.stringify(data); + if ( + !!options.credentials || + (method != "GET" && !options.nocredentials) + ) { + fetchOptions["credentials"] = params.credentials; + } + fetch(params.serverUrl + url, fetchOptions) + .then(res => res.json()) + .then(json => { + if (!json.errmsg && !json.errno) options.success(json); + else { + if (!!json.errmsg) options.error(json.errmsg); + else options.error(json.code + ". errno = " + json.errno); + } + }); }