-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·110 lines (105 loc) · 3.7 KB
/
index.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const response = document.getElementById("response");
const response2 = document.getElementById("response2");
const url = "https://us-central1-cloud-computing-345200.cloudfunctions.net/calculator";
const url2 = "https://us-central1-cloud-computing-345200.cloudfunctions.net/submit-pubsub";
function add() { send('add'); }
function sub() { send("sub"); }
function mul() { send("mul"); }
function div() { send("div"); }
function pow() { send("pow"); }
function timeout() { document.getElementById("response2").innerHTML = ""; }
function get_form_data(op) {
let num1 = document.getElementById("num1").value;
let num2 = document.getElementById("num2").value;
if (num1 == "") { num1 = '0'; }
if (num2 == "") {
switch (op) {
case "add":
num2 = '0';
break;
case "sub":
num2 = '0';
break;
case "mul":
num2 = '1';
break;
case "div":
num2 = '1';
break;
case "pow":
num2 = '1';
break;
}
}
let data = {
'num1': num1,
'num2': num2,
'op': op
};
return data;
}
function send(op) {
let data = get_form_data(op);
let xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader('Content-type', 'application/json');
xmlhttp.send(JSON.stringify(data));
xmlhttp.onreadystatechange = function () {
switch (this.readyState) {
case 0:
response.innerText = "Not initialized connection";
break;
case 1:
response.innerText = "Server connection established";
break;
case 2:
response.innerText = "Server received the request";
break;
case 3:
response.innerText = "Server is processing the request";
break;
case 4:
if (xmlhttp.status != 200) {
response.innerText = this.responseText;
document.getElementById("num1").value = "";
document.getElementById("num2").value = "";
}
else {
response.innerText = this.responseText;
document.getElementById("num1").value = this.responseText;
document.getElementById("num2").value = "";
}
break;
}
}
}
function pubsub() {
let xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url2, true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send(null);
xmlhttp.onreadystatechange = function () {
switch (this.readyState) {
case 0:
response2.innerText = "Not initialized connection";
break;
case 1:
response2.innerText = "Server connection established";
break;
case 2:
response2.innerText = "Server received the request";
break;
case 3:
response2.innerText = "Server is processing the request";
break;
case 4:
if (xmlhttp.status != 200) {
response2.innerText = "Server gave the error " + request.status + ": " + this.responseText;
} else {
response2.innerText = this.responseText;
setTimeout(timeout, 6000);
}
break;
}
}
}