Last active
September 23, 2021 20:03
-
-
Save hanancs/08348bb6e5afa46a0135647515b3d298 to your computer and use it in GitHub Desktop.
All possible ways of making HTTP client requests in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<!-- <script src="scriptfetch.js"></script> --> | |
<!-- <script src="scriptxhr.js"></script> --> | |
<link rel="stylesheet" href="style.css" /> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
</head> | |
<body> | |
<div class="d-flex justify-content-center"> | |
<div class="spinner-border" role="status" id="loading"> | |
<span class="sr-only">Loading...</span> | |
</div> | |
</div> | |
<h1>Registered Employees</h1> | |
<table id="employees"></table> | |
</body> | |
<!-- Axios --> | |
<!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> | |
<script src="scriptaxios.js"></script> --> | |
<!-- Ajax -jQuery --> | |
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | |
<script src="scriptajax.js"></script> --> | |
<script src="https://unpkg.com/superagent"></script> | |
<script src="scriptsuperagent.js"></script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const api_url = "https://jsonplaceholder.typicode.com/users"; | |
async function getapi(url) { | |
await $(document).ready(function () { | |
$.ajax({ | |
url: url, | |
type: "GET", | |
success: function (result) { | |
console.log(result); | |
let data = result; | |
if (result) { | |
hideloader(); | |
} | |
show(data); | |
}, | |
error: function (error) { | |
console.log(error); | |
} | |
}); | |
}); | |
} | |
// Calling that async function | |
getapi(api_url); | |
// Function to hide the loader | |
function hideloader() { | |
document.getElementById('loading').style.display = 'none'; | |
} | |
// Function to define innerHTML for HTML table | |
function show(data) { | |
let tab = | |
`<tr> | |
<th>Name</th> | |
<th>Office</th> | |
<th>Position</th> | |
<th>Salary</th> | |
</tr>`; | |
// Loop to access all rows | |
for (let r of data) { | |
tab += `<tr> | |
<td>${r.name} </td> | |
<td>${r.username}</td> | |
<td>${r.email}</td> | |
<td>${r.website}</td> | |
</tr>`; | |
} | |
// Setting innerHTML as tab variable | |
document.getElementById("employees").innerHTML = tab; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const api_url = "https://jsonplaceholder.typicode.com/users"; | |
//https://masteringjs.io/tutorials/axios/data To change axios response type | |
// https://axios-http.com/docs/intro | |
async function getapi(url) { | |
try { | |
const response = await axios.get(url); | |
console.log(response.data); | |
let data = response.data; | |
if (response) { | |
hideloader(); | |
} | |
show(data); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
// Calling that async function | |
getapi(api_url); | |
// Function to hide the loader | |
function hideloader() { | |
document.getElementById('loading').style.display = 'none'; | |
} | |
// Function to define innerHTML for HTML table | |
function show(data) { | |
let tab = | |
`<tr> | |
<th>Name</th> | |
<th>Office</th> | |
<th>Position</th> | |
<th>Salary</th> | |
</tr>`; | |
// Loop to access all rows | |
for (let r of data) { | |
tab += `<tr> | |
<td>${r.name} </td> | |
<td>${r.username}</td> | |
<td>${r.email}</td> | |
<td>${r.website}</td> | |
</tr>`; | |
} | |
// Setting innerHTML as tab variable | |
document.getElementById("employees").innerHTML = tab; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const api_url = "https://jsonplaceholder.typicode.com/users"; | |
// Defining async function | |
async function getapi(url) { | |
// Storing response | |
const response = await fetch(url); | |
// Storing data in form of JSON | |
var data = await response.json(); | |
console.log(data); | |
if (response) { | |
hideloader(); | |
} | |
show(data); | |
} | |
// Calling that async function | |
getapi(api_url); | |
// Function to hide the loader | |
function hideloader() { | |
document.getElementById('loading').style.display = 'none'; | |
} | |
// Function to define innerHTML for HTML table | |
function show(data) { | |
let tab = | |
`<tr> | |
<th>Name</th> | |
<th>Office</th> | |
<th>Position</th> | |
<th>Salary</th> | |
</tr>`; | |
// Loop to access all rows | |
for (let r of data) { | |
tab += `<tr> | |
<td>${r.name} </td> | |
<td>${r.username}</td> | |
<td>${r.email}</td> | |
<td>${r.website}</td> | |
</tr>`; | |
} | |
// Setting innerHTML as tab variable | |
document.getElementById("employees").innerHTML = tab; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const api_url = "https://jsonplaceholder.typicode.com/users"; | |
//https://visionmedia.github.io/superagent/ | |
function getapi(url) { | |
(async () => { | |
try { | |
const res = await superagent.get(url); | |
console.log(res.body); | |
let data = res.body; | |
if (res) { | |
hideloader(); | |
} | |
show(data); | |
} catch (err) { | |
console.error(err); | |
} | |
})(); | |
} | |
// Calling that async function | |
getapi(api_url); | |
// Function to hide the loader | |
function hideloader() { | |
document.getElementById('loading').style.display = 'none'; | |
} | |
// Function to define innerHTML for HTML table | |
function show(data) { | |
let tab = | |
`<tr> | |
<th>Name</th> | |
<th>Office</th> | |
<th>Position</th> | |
<th>Salary</th> | |
</tr>`; | |
// Loop to access all rows | |
for (let r of data) { | |
tab += `<tr> | |
<td>${r.name} </td> | |
<td>${r.username}</td> | |
<td>${r.email}</td> | |
<td>${r.website}</td> | |
</tr>`; | |
} | |
// Setting innerHTML as tab variable | |
document.getElementById("employees").innerHTML = tab; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const api_url = "https://jsonplaceholder.typicode.com/users"; | |
// Defining async function | |
async function getapi(url) { | |
// Storing response | |
let request = await new XMLHttpRequest(); | |
request.open("GET", url); | |
request.send(); | |
request.onload = () => { | |
// console.log(request); | |
if (request.status === 200) { | |
// by default the response comes in the string format, we need to parse the data into JSON | |
// Storing data in form of JSON | |
var data = JSON.parse(request.response); | |
console.log(data); | |
if (request) { | |
hideloader(); | |
} | |
show(data); | |
} else { | |
console.log(`error ${request.status} ${request.statusText}`); | |
} | |
}; | |
} | |
// Calling that async function | |
getapi(api_url); | |
// Function to hide the loader | |
function hideloader() { | |
document.getElementById('loading').style.display = 'none'; | |
} | |
// Function to define innerHTML for HTML table | |
function show(data) { | |
let tab = | |
`<tr> | |
<th>Name</th> | |
<th>Office</th> | |
<th>Position</th> | |
<th>Salary</th> | |
</tr>`; | |
// Loop to access all rows | |
for (let r of data) { | |
tab += `<tr> | |
<td>${r.name} </td> | |
<td>${r.username}</td> | |
<td>${r.email}</td> | |
<td>${r.website}</td> | |
</tr>`; | |
} | |
// Setting innerHTML as tab variable | |
document.getElementById("employees").innerHTML = tab; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment