Skip to content

Commit 21d6c13

Browse files
Fetch API
1 parent 42b2754 commit 21d6c13

File tree

6 files changed

+443
-0
lines changed

6 files changed

+443
-0
lines changed

15_http_and_forms/02_fetch/data.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "Muhammad Hashim",
3+
"age": 24,
4+
"email": "[email protected]",
5+
"skills": ["JavaScript", "Python", "Java"]
6+
}

15_http_and_forms/02_fetch/data.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is the content of the file.
2+
It could be any text, including paragraphs, information, or even code snippets.

15_http_and_forms/02_fetch/index.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Fetch API Example</title>
7+
</head>
8+
<body>
9+
<h2>Fetch Data Example</h2>
10+
<p id="status"></p>
11+
<p id="content"></p>
12+
13+
<script>
14+
// Asynchronous function to fetch data using async/await
15+
async function fetchData() {
16+
try {
17+
// Make a GET request to fetch the data
18+
const response = await fetch('./data.txt');
19+
20+
// Check if the response is OK (status code 200-299)
21+
if (!response.ok) {
22+
document.getElementById('status').innerText = `Error: ${response.status}`;
23+
return;
24+
}
25+
26+
// Display status code
27+
document.getElementById('status').innerText = `Status: ${response.status}`;
28+
29+
// Fetch the content as text
30+
const text = await response.text();
31+
32+
// Display the content
33+
document.getElementById('content').innerText = text;
34+
} catch (error) {
35+
document.getElementById('status').innerText = `Network error: ${error.message}`;
36+
}
37+
}
38+
39+
// Call the function to fetch data
40+
fetchData();
41+
</script>
42+
</body>
43+
</html>

15_http_and_forms/02_fetch/json.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Fetch JSON Example</title>
7+
</head>
8+
<body>
9+
<h2>Fetch JSON Example</h2>
10+
<pre id="json-content"></pre>
11+
12+
<script>
13+
// Async function to fetch JSON data
14+
async function fetchJsonData() {
15+
try {
16+
const response = await fetch('./data.json');
17+
if (!response.ok) {
18+
document.getElementById('json-content').innerText = `Error: ${response.status}`;
19+
return;
20+
}
21+
22+
// Parse the response as JSON
23+
const jsonData = await response.json();
24+
25+
// Format and display the JSON data with indentation
26+
document.getElementById('json-content').innerText = JSON.stringify(jsonData, null, 2);
27+
} catch (error) {
28+
document.getElementById('json-content').innerText = `Network error: ${error.message}`;
29+
}
30+
}
31+
32+
// Call the function to fetch JSON data
33+
fetchJsonData();
34+
</script>
35+
</body>
36+
</html>

15_http_and_forms/02_fetch/post.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>POST Request Example</title>
7+
</head>
8+
<body>
9+
<h2>POST Data Example</h2>
10+
<p id="post-status"></p>
11+
12+
<script>
13+
// Async function to send a POST request
14+
async function postData() {
15+
try {
16+
const response = await fetch('example/post-endpoint', {
17+
method: 'POST',
18+
headers: {
19+
'Content-Type': 'application/json',
20+
'Authorization': 'Bearer your-token-here'
21+
},
22+
body: JSON.stringify({ name: 'Jane Doe', message: 'Hello World' })
23+
});
24+
25+
if (!response.ok) {
26+
document.getElementById('post-status').innerText = `Error: ${response.status}`;
27+
return;
28+
}
29+
30+
// Display status code if the request was successful
31+
document.getElementById('post-status').innerText = `Status: ${response.status}`;
32+
} catch (error) {
33+
document.getElementById('post-status').innerText = `Network error: ${error.message}`;
34+
}
35+
}
36+
37+
// Call the function to send POST request
38+
postData();
39+
</script>
40+
</body>
41+
</html>

0 commit comments

Comments
 (0)