From 480eeb9ac35959383f5eb30808c12b4ec311782d Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 21 May 2020 20:29:43 -0700 Subject: [PATCH] Update common.js request, get, post functions like Etiquette. --- frontends/ycdl_flask/static/js/common.js | 31 ++++++++++++++++++------ 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/frontends/ycdl_flask/static/js/common.js b/frontends/ycdl_flask/static/js/common.js index df98e9f..4da9606 100644 --- a/frontends/ycdl_flask/static/js/common.js +++ b/frontends/ycdl_flask/static/js/common.js @@ -15,25 +15,42 @@ function null_callback() return; } -common.post = -function post(url, data, callback) +common._request = +function _request(method, url, callback) { var request = new XMLHttpRequest(); - request.answer = null; request.onreadystatechange = function() { if (request.readyState == 4) { - var text = request.responseText; if (callback != null) { - console.log(text); - callback(JSON.parse(text)); + var response = { + "data": JSON.parse(request.responseText), + "meta": {} + }; + response["meta"]["request_url"] = url; + response["meta"]["status"] = request.status; + callback(response); } } }; var asynchronous = true; - request.open("POST", url, asynchronous); + request.open(method, url, asynchronous); + return request; +} + +common.get = +function get(url, callback) +{ + request = common._request("GET", url, callback); + request.send(); +} + +common.post = +function post(url, data, callback) +{ + request = common._request("POST", url, callback); request.send(data); }