X-Git-Url: https://git.auder.net/?a=blobdiff_plain;ds=sidebyside;f=client%2Fsrc%2Futils%2Fajax.js;h=0a50a10409ac1c332b84e141f61a110846b72c0f;hb=1aeed627be63a298d3a093797c3728e3de30b464;hp=46edca06b953068ebeb4391730207db51d9014e9;hpb=f05815d7da84284bd9d7c1ce5b808acd675f2a3e;p=vchess.git diff --git a/client/src/utils/ajax.js b/client/src/utils/ajax.js index 46edca06..0a50a104 100644 --- a/client/src/utils/ajax.js +++ b/client/src/utils/ajax.js @@ -26,16 +26,17 @@ export function ajax(url, method, data, success, error) xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { + let res_json = ""; try { - let res_json = JSON.parse(xhr.responseText); - if (!res_json.errmsg) - success(res_json); - else - error(res_json.errmsg); - } catch (e) { + res_json = JSON.parse(xhr.responseText); + } catch (e) { // Plain text (e.g. for rules retrieval) - success(xhr.responseText); - } + return success(xhr.responseText); + } + if (!res_json.errmsg) + success(res_json); + else + error(res_json.errmsg); } }; @@ -46,7 +47,10 @@ export function ajax(url, method, data, success, error) } xhr.open(method, params.serverUrl + url, true); xhr.setRequestHeader('X-Requested-With', "XMLHttpRequest"); - if (["POST","PUT"].includes(method)) + // Next line because logout and authenticate set (cross-domain in dev mode) cookies + if (url.startsWith("/authenticate") || url.startsWith("/logout")) + xhr.withCredentials = true; + if (["POST","PUT"].includes(method)) { xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.send(JSON.stringify(data));