diff --git a/code/send_request.js b/code/send_request.js new file mode 100644 index 00000000..c951fb80 --- /dev/null +++ b/code/send_request.js @@ -0,0 +1,32 @@ +// posts AJAX request to Ingress API. +// action: last part of the actual URL, the rpc/dashboard. is +// added automatically +// data: JSON data to post. method will be derived automatically from +// action, but may be overridden. Expects to be given Hash. +// Strings are not supported. +// success: method to call on success. See jQuery API docs for avail- +// able arguments: http://api.jquery.com/jQuery.ajax/ +// error: see above. Additionally it is logged if the request failed. +window.postAjax = function(action, data, success, error) { + + var versionStr = nemesis.dashboard.config.CURRENT_VERSION; + var post_data = JSON.stringify($.extend({}, data, {v: versionStr, b: "", c: ""})); + + var remove = function(data, textStatus, jqXHR) { window.requests.remove(jqXHR); }; + var errCnt = function(jqXHR) { window.failedRequestCount++; window.requests.remove(jqXHR); }; + var result = $.ajax({ + url: '/r/'+action, + type: 'POST', + data: post_data, + context: data, + dataType: 'json', + success: [remove, success], + error: error ? [errCnt, error] : errCnt, + contentType: 'application/json; charset=utf-8', + beforeSend: function(req) { + req.setRequestHeader('X-CSRFToken', readCookie('csrftoken')); + } + }); + result.action = action; + return result; +} diff --git a/code/utils_misc.js b/code/utils_misc.js index ab6f6ddb..232613a3 100644 --- a/code/utils_misc.js +++ b/code/utils_misc.js @@ -128,40 +128,6 @@ window.digits = function(d) { } - -// posts AJAX request to Ingress API. -// action: last part of the actual URL, the rpc/dashboard. is -// added automatically -// data: JSON data to post. method will be derived automatically from -// action, but may be overridden. Expects to be given Hash. -// Strings are not supported. -// success: method to call on success. See jQuery API docs for avail- -// able arguments: http://api.jquery.com/jQuery.ajax/ -// error: see above. Additionally it is logged if the request failed. -window.postAjax = function(action, data, success, error) { - - var versionStr = nemesis.dashboard.config.CURRENT_VERSION; - var post_data = JSON.stringify($.extend({}, data, {v: versionStr, b: "", c: ""})); - - var remove = function(data, textStatus, jqXHR) { window.requests.remove(jqXHR); }; - var errCnt = function(jqXHR) { window.failedRequestCount++; window.requests.remove(jqXHR); }; - var result = $.ajax({ - url: '/r/'+action, - type: 'POST', - data: post_data, - context: data, - dataType: 'json', - success: [remove, success], - error: error ? [errCnt, error] : errCnt, - contentType: 'application/json; charset=utf-8', - beforeSend: function(req) { - req.setRequestHeader('X-CSRFToken', readCookie('csrftoken')); - } - }); - result.action = action; - return result; -} - window.zeroPad = function(number,pad) { number = number.toString(); var zeros = pad - number.length;