X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Futils%2Fajax.js;h=83b045c02bbda02ccd87a2bf24f34848ca4dbea5;hb=6f57a9769ad80459d086abf5232d01ec1ce7a470;hp=01ea84935f132ebf7713cbf4f7a242eea3ffe791;hpb=e64c6f67185b45b3b1205069532362c1bf9680db;p=vchess.git diff --git a/client/src/utils/ajax.js b/client/src/utils/ajax.js index 01ea8493..83b045c0 100644 --- a/client/src/utils/ajax.js +++ b/client/src/utils/ajax.js @@ -17,15 +17,16 @@ function toQueryString(data) export function ajax(url, method, data, success, error) { let xhr = new XMLHttpRequest(); - if (typeof(data) === "function") //no data + 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) { @@ -36,10 +37,15 @@ export function ajax(url, method, data, success, error) // Plain text (e.g. for rules retrieval) return success(xhr.responseText); } - if (!res_json.errmsg) + if (!res_json.errmsg && !res_json.errno) success(res_json); else - error(res_json.errmsg); + { + if (!!res_json.errmsg) + error(res_json.errmsg); + else + error(res_json.code + ". errno = " + res_json.errno); + } } };