Javascript Fetch Api

Hello guys today i am going to show you a javascript built in feature which has better way to call ajax without using jquery library.

Checkout my below code which gets github users using fetch api.

Click on “Load Data” Button and you will see first Name in alert box. That first name has been read from github’s user Api.

Code is as Below:

document.querySelector("#js-load-data").addEventListener("click",function(){
fetch("https://api.github.com/users").then(function(result) {
return result.json();
}).then(function(data)
{
alert("First name is : " + data[0].login);
})
.catch(function(error) {
console.log(error)
})
});
Blog Catagory : JAVASCRIPT