-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax1.html
64 lines (55 loc) · 1.96 KB
/
ajax1.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 lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<button id="button">Click me</button> <br />
<div id="text"></div>
<script>
// create event listener
document.getElementById("button").addEventListener("click", loadText);
function loadText() {
// create simple XHR object
console.log("button clicked");
var xhr = new XMLHttpRequest();
// open - type, url/file, asyc
console.log(xhr);
xhr.open("GET", "sample.txt", true);
console.log("READYSTATE", xhr.readyState);
// optional for loaders
xhr.onprogress = function() {
console.log("READY STATE ON PROGRESS", xhr.readyState);
};
xhr.onload = function() {
if (xhr.status == 200) {
console.log("READY STATE ON LOAD", this.responseText);
document.getElementById("text").innerHTML = this.responseText;
} else if ((this.status = 404)) {
document.getElementById("text").innerHTML = "Not Found";
}
};
xhr.onerror = function() {
console.log("READY STATE ON ERROR", xhr.onerror);
};
xhr.onreadystatechange = function() {
console.log("READY STATE ON REAFY STATE CHANGE", xhr.readyState);
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhr.send();
}
</script>
</body>
</html>
<!--
0 UNSENT Client has been created. open() not called yet.
1 OPENED open() has been called.
2 HEADERS_RECEIVED send() has been called, and headers and status are available.
3 LOADING Downloading; responseText holds partial data.
-->