forked from tashaxing/CppHttpDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
90 lines (79 loc) · 2.13 KB
/
Copy pathindex.html
File metadata and controls
90 lines (79 loc) · 2.13 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
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
<!DOCTYPE html>
<html>
<head>
<title>RESTful API demo</title>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
// simple http
$(document).ready(function(){
$("button").click(function(){
$.get("/api/hello",function(data, status){
console.log("get rsp: ", data);
$('#result1').html(data);
});
});
});
$(document).on('keyup', '#n1, #n2', function() {
$.ajax({
url: '/api/sum',
method: 'POST',
dataType: 'json',
data: { n1: $('#n1').val(), n2: $('#n2').val() },
success: function(json) {
console.log("post rsp: ", json);
$('#result2').html(json.result);
}
});
});
// websocket
var websocket = new WebSocket('ws://' + location.host + '/ws');
websocket.onopen = function (ev) {
console.log(ev.data);
};
websocket.onerror = function (ev) {
console.log(ev.data);
};
websocket.onclose = function (ev) {
console.log(ev.data);
};
websocket.onmessage = function (ev) {
console.log(ev.data);
document.getElementById("ws_text").innerHTML = ev.data;
};
window.onload = function () {
document.getElementById('send_button').onclick = function (ev) {
var msg = document.getElementById('send_input').value;
websocket.send(msg);
};
};
</script>
</head>
<body>
<h1>c++ httpserver demo</h1>
<h2>simple http</h2>
<h3>GET</h3>
<div>
<button id="btn">get request</button>
</div>
<div>
<label>Result1:</label> <span id="result1"> </span>
</div>
<h3>POST</h3>
<div>
<label>Number 1:</label> <input type="text" id="n1" />
</div>
<div>
<label>Number 2:</label> <input type="text" id="n2" />
</div>
<div>
<label>Result2:</label> <span id="result2"> </span>
</div>
<h2>websocket</h2>
<div>
<span id="ws_text"> </span>
<br />
<input type="text" id="send_input" />
<button id="send_button">Send</button>
</div>
</body>
</html>