aboutsummaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2016-04-24 22:25:17 +0200
committerpiernov <piernov@piernov.org>2016-04-24 22:25:17 +0200
commitf2d53d539ee55e1b41cc74368ad5457da6e05cf3 (patch)
treeccdf0641555cc4c7c895c69077a4e8011a63c0fb /js
parent7f345404a79e6e0f25bafa6f25c8ba1edf6aef73 (diff)
downloadcandybox-f2d53d539ee55e1b41cc74368ad5457da6e05cf3.tar.gz
candybox-f2d53d539ee55e1b41cc74368ad5457da6e05cf3.tar.bz2
candybox-f2d53d539ee55e1b41cc74368ad5457da6e05cf3.tar.xz
candybox-f2d53d539ee55e1b41cc74368ad5457da6e05cf3.zip
Decode JSON before calling callback and prevent call in case of error
Diffstat (limited to 'js')
-rw-r--r--js/craftmine.js10
1 files changed, 7 insertions, 3 deletions
diff --git a/js/craftmine.js b/js/craftmine.js
index 46c726a..912d10c 100644
--- a/js/craftmine.js
+++ b/js/craftmine.js
@@ -11,7 +11,11 @@ function sendRequest(url, params, callback) {
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") {
- callback(xhr);
+ var data = JSON.parse(xhr.responseText);
+ if(data.error) {
+ return;
+ }
+ if(callback) callback(data);
}
}
xhr.send(params);
@@ -31,7 +35,7 @@ function debitAccount(amount) {
function withdrawMine() {
sendRequest("craftmine.php", "op=withdrawMine&amount="+data.mine, function(xhr) {
- var gold = parseInt(xhr.responseText); // Server's response is a string
+ var gold = parseInt(xhr); // Server's response is a string
if(isNaN(gold)) return;
data.gold = gold;
data.mine = 0;
@@ -41,7 +45,7 @@ function withdrawMine() {
function initCraftMine() {
sendRequest("craftmine.php", "op=getCraftMine", function(xhr) {
- var ret = xhr.responseText;
+ var ret = xhr;
data.gold = parseInt(ret); // Server's response is a string
updateData("gold");
})