From f2d53d539ee55e1b41cc74368ad5457da6e05cf3 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:25:17 +0200 Subject: Decode JSON before calling callback and prevent call in case of error --- js/craftmine.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'js') 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"); }) -- cgit v1.2.3-54-g00ecf