projects
/
vchess.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix authentication
[vchess.git]
/
client
/
src
/
utils
/
ajax.js
diff --git
a/client/src/utils/ajax.js
b/client/src/utils/ajax.js
index
065f382
..
1d181fa
100644
(file)
--- a/
client/src/utils/ajax.js
+++ b/
client/src/utils/ajax.js
@@
-1,3
+1,5
@@
+import params from "../parameters"; //for server URL
+
// From JSON (encoded string values!) to "arg1=...&arg2=..."
function toQueryString(data)
{
// From JSON (encoded string values!) to "arg1=...&arg2=..."
function toQueryString(data)
{
@@
-24,16
+26,17
@@
export function ajax(url, method, data, success, error)
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
+ let res_json = "";
try {
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)
// 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);
}
};
}
};
@@
-42,9
+45,11
@@
export function ajax(url, method, data, success, error)
// Append query params to URL
url += "/?" + toQueryString(data);
}
// Append query params to URL
url += "/?" + toQueryString(data);
}
- xhr.open(method, url, true);
+ xhr.open(method,
params.serverUrl +
url, true);
xhr.setRequestHeader('X-Requested-With', "XMLHttpRequest");
xhr.setRequestHeader('X-Requested-With', "XMLHttpRequest");
- if (["POST","PUT"].includes(method))
+ // Next line to allow cross-domain cookies in dev mode (TODO: if...)
+ xhr.withCredentials = true;
+ if (["POST","PUT"].includes(method))
{
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(data));
{
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(data));