Skip to content

Commit 0b4e23e

Browse files
committed
Simple Text
1 parent 1905d6a commit 0b4e23e

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# AJAX
22

33
Asynchronous JavaScript and XML
4+
5+
HTTP Requests - Communication between Client and Server
6+
7+
API - Application Programming Interface
8+
9+
- Set of Instructions to work with Server i.e GET, POST, PUT, DELETE resource from the server.

api/sample.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Iusto, optio. Odio omnis non officia tenetur, at rem assumenda qui! Veniam libero fugit tempore assumenda tempora at, et placeat quasi nisi.

app.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
console.log("AJAX");
1+
const xhr = new XMLHttpRequest();
2+
3+
xhr.open("GET", "./api/sample.txt");
4+
5+
/* It gets fired when since ready state changes from 0 to 1 when used open() method */
6+
xhr.onreadystatechange = function () {
7+
// console.log(xhr);
8+
if (xhr.readyState === 4 && xhr.status === 200) {
9+
const paragraph = document.createElement("p");
10+
paragraph.textContent = xhr.responseText;
11+
document.body.appendChild(paragraph);
12+
} else {
13+
console.log({
14+
status: xhr.status,
15+
statusText: xhr.statusText,
16+
state: xhr.readyState,
17+
});
18+
}
19+
};
20+
21+
/* It sends the request to server, then it displays on the browser */
22+
xhr.send();
23+
24+
// It executes first, since the above functionality works asynchronously by browser, only when JavaScript is free then it takes the data from browser
25+
console.log("hello world");

0 commit comments

Comments
 (0)