/
example.html
55 lines (48 loc) · 1.11 KB
/
example.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
<!doctype html>
<html>
<head>
<script>
function new_request() {
var req;
req = new XMLHttpRequest();
return req;
}
// Simple blocking JSON-RPC function
function rpc(method) {
// Get params (argument 1 and out)
var args = Array.prototype.slice.call(arguments);
var params = args.slice(1);
if(params === undefined) {
params = []; // Allow caller to leave out params
}
var i;
var req = new_request();
req.open('POST', 'rpc', false);
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify({
'method' : method,
'params' : params,
'id' : 1
}));
if(req.status == 200) {
var ret = JSON.parse(req.responseText);
if (ret.error !== null) {
console.log(ret.error);
return undefined;
} else {
return ret.result;
}
} else {
return undefined;
}
}
</script>
</head>
<body>
<p>
1 + 2 == <script>document.write(rpc('add', 1, 2));</script>
<br>
sort([3, 1, 2]) == [<script>document.write(rpc('sort', [3, 1, 2]));</script>]
</p>
</body>
</html>