-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathThreadedAjax.html
76 lines (68 loc) · 1.76 KB
/
ThreadedAjax.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
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html>
<head>
<title>Ozai</title>
<style>
#node {
width: 400px;
height: 300px;
overflow-y: scroll;
border: 3px solid black;
font-size: 10px;
}
html, body {
font-family: Verdana, sans-serif;
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<h1>(GET) Node.js API</h1>
<div id="node"></div>
</body>
<script src="ozai.js"></script>
<script>
//Create a worker to perform AJAX requests in the background.
//Origin is needed because the URL of the worker is NOT
//the same as the URL of the document.
var ajaxWorker = new Ozai({
origin: window.location.origin,
get: function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState==4 && xhr.status==200) {
callback(xhr.responseText);
}
}
//if posting to a relative path, append the origin
if(url.indexOf("//") < 0) {
url = this.origin + url;
}
xhr.open("GET", url, true);
xhr.send();
},
post: function(url, data, callback) {
var xhr = new XMLHttpRequest();
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState==4 && xhr.status==200) {
callback(xhr.responseText);
}
}
//if posting to a relative path, append the origin
if(url.indexOf("//") < 0) {
url = this.origin + url;
}
xhr.open("POST", this.origin + url, true);
xhr.send(JSON.stringify(data));
}
});
//Using the ajaxWorker, perform a GET request to the node.js API...
ajaxWorker.get("http://nodejs.org/api/index.json", function(responseText) {
document.getElementById("node").innerHTML = responseText;
});
</script>
</html>