Sync common.js with Etiquette.

This commit is contained in:
voussoir 2020-11-08 19:32:04 -08:00
parent be7c5982e9
commit 5841279f9e

View file

@ -40,6 +40,17 @@ function refresh()
window.location.reload(); window.location.reload();
} }
common.refresh_or_alert =
function refresh_or_alert(response)
{
if (response.meta.status !== 200)
{
alert(JSON.stringify(response));
return;
}
window.location.reload();
}
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// HTTP //////////////////////////////////////////////////////////////////////////////////////////// // HTTP ////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -52,8 +63,8 @@ function _request(method, url, callback)
The response will have the following structure: The response will have the following structure:
{ {
"completed": true / false,
"meta": { "meta": {
"completed": true / false,
"status": If the connection failed or request otherwise could not "status": If the connection failed or request otherwise could not
complete, `status` will be 0. If the request completed, complete, `status` will be 0. If the request completed,
`status` will be the HTTP response code. `status` will be the HTTP response code.
@ -63,13 +74,12 @@ function _request(method, url, callback)
and `response.data` will be undefined. and `response.data` will be undefined.
"request_url": The URL exactly as given to this call. "request_url": The URL exactly as given to this call.
} }
"data": {JSON parsed from server response}. "data": {JSON parsed from server response if json_ok}.
} }
*/ */
const request = new XMLHttpRequest(); const request = new XMLHttpRequest();
const response = { const response = {
"completed": false, "meta": {"completed": false, "status": 0},
"meta": {"status": 0},
}; };
request.onreadystatechange = function() request.onreadystatechange = function()
@ -93,7 +103,7 @@ function _request(method, url, callback)
if (request.status != 0) if (request.status != 0)
{ {
response.completed = true; response.meta.completed = true;
try try
{ {
response.data = JSON.parse(request.responseText); response.data = JSON.parse(request.responseText);