Update common.js request, get, post functions like Etiquette.

This commit is contained in:
voussoir 2020-05-21 20:29:43 -07:00
parent 5c4b2e9d21
commit 480eeb9ac3

View file

@ -15,25 +15,42 @@ function null_callback()
return; return;
} }
common.post = common._request =
function post(url, data, callback) function _request(method, url, callback)
{ {
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.answer = null;
request.onreadystatechange = function() request.onreadystatechange = function()
{ {
if (request.readyState == 4) if (request.readyState == 4)
{ {
var text = request.responseText;
if (callback != null) if (callback != null)
{ {
console.log(text); var response = {
callback(JSON.parse(text)); "data": JSON.parse(request.responseText),
"meta": {}
};
response["meta"]["request_url"] = url;
response["meta"]["status"] = request.status;
callback(response);
} }
} }
}; };
var asynchronous = true; 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); request.send(data);
} }