diff --git a/js/api/ServerAPI.js b/js/api/ServerAPI.js index 00ac9dd..1e0edcf 100644 --- a/js/api/ServerAPI.js +++ b/js/api/ServerAPI.js @@ -27,12 +27,56 @@ function ServerAPI() { if (completeHandler) completeHandler(response); } }).fail(function(response) { - console.log("fail"); - if (failedHandler) failedHandler(); + if (failedHandler) failedHandler(response.responseJSON); }); }; - // '/fetch/'+uid.value+'/'+line.value; + this.fetch = function(uuid, chunk, completeHandler, failedHandler) { + console.log("ServerAPI:fetch",uuid,chunk); + if (!uuid) { + console.log("Server.fetch failed: no uuid"); + if (failedHandler) failedHandler(); + return; + } + + if (!chunk) { + chunk = 0; + } + + $.ajax({ + url: _apiURL + "/fetch/" + uuid + "/" + chunk, + type: "GET", + dataType: 'text', //no json but text + timeout: _timeoutTime, + cache: false, + success: function(response) { + if (completeHandler) completeHandler(response); + } + }).fail(function(response) { + console.log("ServerAPI:fetch fail response:",response); + if (failedHandler) failedHandler(response.responseJSON); + }); + }; + + this.fetchHeader = function(uuid,completeHandler,failedHandler) { + this.fetch(uuid,0,function(successData) { + console.log("_serverAPI fetchHeader"); + if (successData) { + var items = successData.split("\n"); + try { + var headerText = items[0].substr(1); + var headerJSON = JSON.parse(headerText); + // console.log("fetchHeader",headerJSON) + if (completeHandler) completeHandler(headerJSON); + } catch (e) { + console.log("fetchHeader failed to parse JSON",e); + if (failedHandler) failedHandler(); + } + } + }, function(failData) { + console.log("_serverAPI fetchHeader failed",failData); + }); + }; }