-
Notifications
You must be signed in to change notification settings - Fork 22
/
addIndex.html
64 lines (52 loc) · 1.83 KB
/
addIndex.html
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
<!DOCTYPE html>
<html>
<!-- Sends a calculation to a server to perform a calculation
Used in conjunction with example CH11-14 Addition App Server which
hosts this file and sends it to the client.
Code in this file will request a calculation by building an HTML
query and sending it to the host. Note that it is configured to use a
localhost address which means that the server must be running on
the local machine for this to work.
-->
<head>
<title>Ch11-14 Addition App Client</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>A networked Adding Machine</h1>
<p>Uses a server to add two numbers togther.</p>
<p>
First number: <input type="text" id="no1Text" value="0">
</p>
<p>
Second number: <input type="text" id="no2Text" value="0">
</p>
<p>
<button onclick="doAddition()" class="calcButton">Add numbers</button>
</p>
<p id="resultParagraph">
Result displayed here.
</p>
<script>
function doAddition() {
var no1Element = document.getElementById("no1Text");
var no1Text = no1Element.value;
var no2Element = document.getElementById("no2Text");
var no2Text = no2Element.value;
// build a url containing the query
var url = "http://localhost:8080/docalc.html?no1="+no1Text+"&no2="+no2Text;
// send the request to the server to do the sum
fetch(url).then(response => {
if (!response.ok) {
alert("Fetch failed");
return;
}
response.text().then(result => {
let resultParagraph = document.getElementById('resultParagraph');
resultParagraph.innerText = "Result: " + result;
}).catch(error => alert("Bad text: " + error)); // text error handler
}).catch(error => alert("Bad fetch: " + error)); // fetch error handler
}
</script>
</body>
</html>