Skip to content

Commit 84b7376

Browse files
JavaScript API Calling Code & Reference Added
1 parent e95470d commit 84b7376

File tree

5 files changed

+328
-0
lines changed

5 files changed

+328
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*Title: XMLHttpRequest
2+
Description: API Calling using XMLHttpRequest
3+
Author: Md. Samiur Rahman (Mukul)
4+
Website: http://www.SamiurRahmanMukul.epizy.com
5+
Github: https://www.github.com/SamiurRahmanMukul
6+
Email: example2022@gmail.com [FAKE EMAIL]
7+
Date: 06/12/2021 */
8+
9+
/* // ? API Calling using XMLHttpRequest -->
10+
event - onload(), onerror()
11+
property - response, responseText, responseType, responseURL, status, statusText
12+
function - open(), send(), setRequestHeader() */
13+
14+
const makeRequest = (method, url, data) => {
15+
return new Promise((resolve, reject) => {
16+
const xhr = new XMLHttpRequest();
17+
xhr.open(method, url);
18+
19+
xhr.setRequestHeader("Content-Type", "application/json");
20+
21+
xhr.onload = () => {
22+
let data = xhr.response;
23+
console.log(JSON.parse(data));
24+
};
25+
26+
xhr.onerror = () => {
27+
console.log("error is here");
28+
};
29+
30+
xhr.send(JSON.stringify(data));
31+
});
32+
};
33+
34+
// ? make a getData() function to get data from the API server
35+
const getData = () => {
36+
makeRequest("GET", "https://jsonplaceholder.typicode.com/posts").then((res) => console.log(res));
37+
};
38+
39+
getData();
40+
41+
// ? make a postData() function to post data to the API server
42+
const postData = () => {
43+
makeRequest("POST", "https://jsonplaceholder.typicode.com/posts", {
44+
title: "foo",
45+
body: "bar",
46+
userId: 1,
47+
});
48+
};
49+
50+
// postData();
51+
52+
// ? make a updateData() function to update data to the API server
53+
const updateData = () => {
54+
makeRequest("PUT", "https://jsonplaceholder.typicode.com/posts/1", {
55+
id: 1,
56+
title: "fooMA",
57+
body: "barMA",
58+
userId: 1,
59+
});
60+
};
61+
62+
// updateData();
63+
64+
// ? make a updateSingleData() function to update single data to the API server
65+
const updateSingleData = () => {
66+
makeRequest("PATCH", "https://jsonplaceholder.typicode.com/posts/1", {
67+
title: "This is changed",
68+
});
69+
};
70+
71+
// updateSingleData();
72+
73+
// ? make a deleteData() function to delete data to the API server
74+
const deleteData = () => {
75+
makeRequest("DELETE", "https://jsonplaceholder.typicode.com/posts/1");
76+
};
77+
78+
// deleteData();
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*Title: Fetch API
2+
Description: Fetch API - JavaScript API for making HTTP requests
3+
Author: Md. Samiur Rahman (Mukul)
4+
Website: http://www.SamiurRahmanMukul.epizy.com
5+
Github: https://www.github.com/SamiurRahmanMukul
6+
Email: example2022@gmail.com [FAKE EMAIL]
7+
Date: 06/12/2021 */
8+
9+
/* // ? Fetch API - Fetching data from a server
10+
fetch() has replaced XMLHttpRequest
11+
fetch() - global method for making HTTP Request
12+
2 ways to call - then, async await
13+
14+
+ fetch() is easy to use compare to XMLHttpRequest
15+
+ fetch() returns a promise
16+
- returned promise can only handle network error
17+
- does not support all the older browser */
18+
19+
// ? method for making HTTP Request
20+
const makeRequest = async (url, config) => {
21+
const res = await fetch(url, config);
22+
23+
if (!res.ok) {
24+
const message = `Error : ${res.status}`;
25+
throw new Error(message);
26+
}
27+
28+
const data = await res.json();
29+
return data;
30+
};
31+
32+
// ? make a getData() function to get data from the API server
33+
const getData = () => {
34+
makeRequest("https://jsonplaceholder.typicode.com/posts")
35+
.then((res) => console.log(res))
36+
.catch((err) => console.log(err));
37+
};
38+
39+
getData();
40+
41+
// ? make a sendData() function to send data to the API server
42+
const sendData = () => {
43+
makeRequest("https://jsonplaceholder.typicode.com/posts", {
44+
method: "POST",
45+
body: JSON.stringify({
46+
title: "foo",
47+
body: "bar",
48+
userId: 1,
49+
}),
50+
headers: {
51+
"Content-type": "application/json; charset=UTF-8",
52+
},
53+
})
54+
.then((res) => console.log(res))
55+
.catch((err) => console.log(err));
56+
};
57+
58+
// sendData();
59+
60+
// ? make a updateData() function to update data to the API server
61+
const updateData = () => {
62+
makeRequest("https://jsonplaceholder.typicode.com/posts/1", {
63+
method: "PATCH",
64+
body: JSON.stringify({
65+
title: "Hello World",
66+
}),
67+
headers: {
68+
"Content-type": "application/json; charset=UTF-8",
69+
},
70+
})
71+
.then((res) => console.log(res))
72+
.catch((err) => console.log(err));
73+
};
74+
75+
// updateData();
76+
77+
// ? make a deleteData() function to delete data to the API server
78+
const deleteData = () => {
79+
makeRequest("https://jsonplaceholder.typicode.com/posts/1", {
80+
method: "DELETE",
81+
})
82+
.then((res) => console.log(res))
83+
.catch((err) => console.log(err));
84+
};
85+
86+
// deleteData();
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*Title: Axios JS Library
2+
Description: Axios JS Library - API calling and data fetch
3+
Author: Md. Samiur Rahman (Mukul)
4+
Website: http://www.SamiurRahmanMukul.epizy.com
5+
Github: https://www.github.com/SamiurRahmanMukul
6+
Email: example2022@gmail.com [FAKE EMAIL]
7+
Date: 06/12/2021 */
8+
9+
/* // ? axios --> is a JavaScript library
10+
it helps to make request from browser (Plain JS/Vue/React/Angular), Node.js
11+
+ very easy to use
12+
+ it supports all modern browser including IE
13+
+ it returns promise
14+
+ throws error brilliantly
15+
+ No need to set header cause axios is intelligent */
16+
17+
/* // ? axios --> property & methods
18+
axios(config)
19+
axios(url [, config])
20+
21+
axios.get(url [, config])
22+
axios.post(url [, config])
23+
axios.put(url [, config])
24+
axios.patch(url [, config])
25+
axios.delete(url [, config])
26+
27+
axios returns response object - data, status, statusText, headers, config */
28+
const makeRequest = async (config) => {
29+
return await axios(config);
30+
};
31+
32+
// ? make a getData() function to get data from the API server
33+
const getData = () => {
34+
makeRequest("https://jsonplaceholder.typicode.com/posts")
35+
.then((res) => console.log(res))
36+
.catch((err) => console.log(err));
37+
};
38+
39+
getData();
40+
41+
// ? make a createData() function to post data to the API server
42+
const createData = () => {
43+
makeRequest({
44+
url: "https://jsonplaceholder.typicode.com/posts",
45+
method: "post",
46+
data: JSON.stringify({
47+
title: "foo",
48+
body: "bar",
49+
userId: 1,
50+
}),
51+
})
52+
.then((res) => console.log(res))
53+
.catch((err) => console.log(err));
54+
};
55+
56+
// createData();
57+
58+
// ? make a updateData() function to update data to the API server
59+
const updateData = () => {
60+
makeRequest({
61+
url: "https://jsonplaceholder.typicode.com/posts/1",
62+
method: "put",
63+
data: JSON.stringify({
64+
id: 1,
65+
title: "This is a title",
66+
body: "This is a body",
67+
userId: 1,
68+
}),
69+
})
70+
.then((res) => console.log(res.data))
71+
.catch((err) => console.log(err));
72+
};
73+
74+
// updateData();
75+
76+
// ? make a deleteData() function to delete data to the API server
77+
const deleteData = () => {
78+
makeRequest({
79+
url: "https://jsonplaceholder.typicode.com/posts/1",
80+
method: "delete",
81+
})
82+
.then((res) => console.log(res.data))
83+
.catch((err) => console.log(err));
84+
};
85+
86+
// deleteData();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*Title: jQuery Ajax
2+
Description: jQuery Ajax is used to API calling and data fetching
3+
Author: Md. Samiur Rahman (Mukul)
4+
Website: http://www.SamiurRahmanMukul.epizy.com
5+
Github: https://www.github.com/SamiurRahmanMukul
6+
Email: example2022@gmail.com [FAKE EMAIL]
7+
Date: 06/12/2021 */
8+
9+
/* // ? jQuery Ajax -->
10+
add jquery library cdn
11+
ajax - asynchronous javascript and xml */
12+
13+
const makeRequest = async (url, method, data) => {
14+
try {
15+
const result = await $.ajax({
16+
url: url,
17+
method: method,
18+
data: data,
19+
});
20+
return result;
21+
} catch (err) {
22+
console.log(err);
23+
}
24+
};
25+
26+
// ? make a getData() function to get data from API server
27+
const getData = () => {
28+
makeRequest("https://jsonplaceholder.typicode.com/posts/1", "GET").then((res) => console.log(res));
29+
};
30+
31+
getData();
32+
33+
// ? make a createData() function to post data to API server
34+
const createData = () => {
35+
makeRequest("https://jsonplaceholder.typicode.com/posts/1", "PUT", {
36+
id: 1,
37+
title: "This is a title",
38+
body: "This is a body",
39+
userId: 1,
40+
}).then((res) => console.log(res));
41+
};
42+
43+
// createData();
44+
45+
// ? make a deleteData() function to delete data from API server
46+
const deleteData = () => {
47+
makeRequest("https://jsonplaceholder.typicode.com/posts/1", "DELETE").then((res) => console.log(res));
48+
};
49+
50+
// deleteData();

8-JavaScript-API-Calling/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# JavaScript API (Application Programming Interface) Calling
2+
3+
JavaScript API - JavaScript API is a set of functions that are used to perform certain tasks in JavaScript.
4+
5+
## API Calling in JavaScript - 4 way
6+
7+
- 1. XML HTTP Request
8+
- 2. Fetch API
9+
- 3. Axios JavaScript Library
10+
- 4. jQuery Ajax
11+
12+
## Fetch API - API using fetching data from a server
13+
14+
```sh
15+
const loadData = () => {
16+
fetch("https://jsonplaceholder.typicode.com/posts")
17+
.then((response) => response.json())
18+
.then((data) => {
19+
let lists = "";
20+
data.map((data, index) => {
21+
lists += `<li>${data.title}</li>`;
22+
});
23+
document.querySelector(".container ol").innerHTML = lists;
24+
});
25+
};
26+
27+
loadData();
28+
```

0 commit comments

Comments
 (0)