projects
/
vchess.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix error handling in ajax() function
[vchess.git]
/
client
/
src
/
utils
/
ajax.js
diff --git
a/client/src/utils/ajax.js
b/client/src/utils/ajax.js
index
01ea849
..
83b045c
100644
(file)
--- 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();
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 = {};
}
{
error = success;
success = data;
data = {};
}
+ if (!success)
+ success = () => {}; //by default, do nothing
if (!error)
error = errmsg => { alert(errmsg); };
if (!error)
error = errmsg => { alert(errmsg); };
-
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
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);
}
// 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
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);
+ }
}
};
}
};