X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Futils%2Fajax.js;h=e539f4685d177e0c443fe69145709bee2a132786;hb=6808d7a16ec1e761c6a2dffec2281c96953e4d89;hp=eb30330a7b7bb9a77ff1932819bd45af4777c129;hpb=317b8a5610953b30cfb84382bd13764177ce830b;p=vchess.git diff --git a/client/src/utils/ajax.js b/client/src/utils/ajax.js index eb30330a..e539f468 100644 --- a/client/src/utils/ajax.js +++ b/client/src/utils/ajax.js @@ -5,66 +5,57 @@ import params from "../parameters"; //for server URL // Problem: fetch() does not set req.xhr... see access/ajax() security especially for /whoami // 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 (data === undefined || typeof(data) === "function") //no data - { - error = success; - success = data; - data = {}; - } - if (!success) - success = () => {}; //by default, do nothing - if (!error) - error = errmsg => { alert(errmsg); }; - xhr.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) - { +export function ajax(url, method, data, success, error) { + let xhr = new XMLHttpRequest(); + if (data === undefined || typeof data === "function") { + //no data + error = success; + success = data; + data = {}; + } + if (!success) success = () => {}; //by default, do nothing + if (!error) + error = errmsg => { + alert(errmsg); + }; + xhr.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { let res_json = ""; - try { - res_json = JSON.parse(xhr.responseText); + try { + res_json = JSON.parse(xhr.responseText); } catch (e) { - // Plain text (e.g. for rules retrieval) - return success(xhr.responseText); + // Plain text (e.g. for rules retrieval) (TODO: no more plain text in current version) + success(xhr.responseText); } - if (!res_json.errmsg && !res_json.errno) - success(res_json); - else - { - if (!!res_json.errmsg) - error(res_json.errmsg); - else - error(res_json.code + ". errno = " + res_json.errno); + if (res_json) { + if (!res_json.errmsg && !res_json.errno) success(res_json); + else { + if (res_json.errmsg) error(res_json.errmsg); + else error(res_json.code + ". errno = " + res_json.errno); + } } - } - }; + } + }; - if (["GET","DELETE"].includes(method) && !!data) - { - // Append query params to URL - url += "/?" + toQueryString(data); - } - xhr.open(method, params.serverUrl + url, true); - xhr.setRequestHeader('X-Requested-With', "XMLHttpRequest"); - // Next line to allow cross-domain cookies in dev mode (TODO: if...) - if (params.cors) - xhr.withCredentials = true; - if (["POST","PUT"].includes(method)) - { - xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); - xhr.send(JSON.stringify(data)); - } - else - xhr.send(); + if (["GET", "DELETE"].includes(method) && !!data) { + // Append query params to URL + url += "/?" + toQueryString(data); + } + xhr.open(method, params.serverUrl + url, true); + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + // Next line to allow cross-domain cookies in dev mode + if (params.cors) xhr.withCredentials = true; + if (["POST", "PUT"].includes(method)) { + xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + xhr.send(JSON.stringify(data)); + } else xhr.send(); }