Files

53 lines
1.6 KiB
JavaScript

class ControllerBase {
ProcessResponse(response) {
alert("Вызов нереализованного метода. Сообщите об этой ошибке разработчикам");
}
constructor(path) {
this.path = path;
}
JsonToUrlParameters(data) {
return new URLSearchParams(data).toString();
}
async Get(parameters) {
let path = this.path + "?" + this.JsonToUrlParameters(parameters);
console.log(`['${path}', 'get'] requesting`);
const response = await fetch(path, {
method: "GET"
});
const response_body = await response.json();
console.log(`['${this.path}', 'get'] response: '${JSON.stringify(response_body)}'`)
this.ProcessResponseImpl(response_body);
}
async Any(request, method) {
request = JSON.stringify(request);
console.log(`['${this.path}', '${method}'] sending: '${request}'`);
const response = await fetch(this.path, {
method: method,
// mode: 'cors',
// headers: { 'Content-Type': 'application/json' },
body: request
});
const response_body = await response.json();
console.log(`['${this.path}', '${method}'] response: '${JSON.stringify(response_body)}'`)
this.ProcessResponseImpl(response_body);
}
async ProcessResponseImpl(response) {
if (response["status"] != "ok") {
alert('Возникла ошибка. Сообщите о ней разработчикам');
} else {
this.ProcessResponse(response);
}
}
}