-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiRequest.js
More file actions
36 lines (34 loc) · 1.02 KB
/
apiRequest.js
File metadata and controls
36 lines (34 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { SurveySystemException } from "./exception.js";
import { displayChart } from "./chart.js";
class ApiRequestError extends SurveySystemException {
constructor(msg) {
super(msg);
}
}
export class ApiRequest {
constructor(formData) {
this._API_SERVER_PATH = "./api/index.php";
this._formData = formData;
}
_updateResult(response) {
displayChart(JSON.parse(response));
}
sendForm() {
const xhr = new XMLHttpRequest();
xhr.timeout = 2000;
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
const status = xhr.status;
if (status === 0 || (status >= 200 && status < 400)) {
this._updateResult(xhr.responseText);
} else {
const apiRequestError = new ApiRequestError("HTTP request failed");
apiRequestError.addError(`Server replied with status ${status}`);
apiRequestError.displayErrorWindow();
}
}
};
xhr.open("POST", this._API_SERVER_PATH);
xhr.send(this._formData);
}
}