AJAX and Fetch explained in simple words
Tags: api, fetch, json, ajax
Published: 2019-11-18 02:33:55

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/872nn3pz1jdl4pdqmebz.jpg)
<h3>In this article I'd like to share with you 2 simple templates that will help you grab data from servers</h3>
<p>They are copy-paste ready-to-use templates. Some changes need to be done depending on how you want to use received data. In my examples I explain basic principles of retrieving data from servers just logging it to a console. Everything is explained with simple terms and words.</p>
<p>All data is stored on servers in JSON format. They are always objects consisting of strings.</p>
<p>The URL with JSON data that I'm using for my examples is: https://jsonplaceholder.typicode.com/users</p>
<h1>AJAX / XMLHttpRequest</h1>
<p>This code sends a GET request to a server. It means that we just receive some data from a server without sending anything back and not modifying any data stored on a server</p>
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/r1rl3lyo4lxx8fhx3j39.jpeg)
On line 1 we save a URL which we are sending our request to.
On line 2 we create a new XMLHttpRequest and give it a name 'xhr'.
On lines 10-11 we open and send our request to a server.
<p>Inside the function on lines 4-9 we write everything that we want to do with the data that we receive from a server.</p>
<p>Data is returned from a server as a JSON object. All data inside JSON objects is always a string. To be able to use this data we need to convert it into a JavaScript object using JSON.parse() method. We do that on line 6 and save it into a variable 'res'. Now the data stored in the 'res' variable is an array of JavaScript objects represented by key:value pairs. Each of the objects in our array looks like this:</p>
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/sc15ek29yc6t6mk7fv7f.jpeg)
<p>After that we use map() method and a callback function passed in inside map() to iterate over each object inside an array stored in a 'res' variable and grab data that we need.</p>
<p>The callback function in our case is an arrow function. It's parameter 'item' represents each individual object.</p>
<p>As we could see in the picture above each object has multiple keys ('address', 'company', 'email', etc). By appending key names to our parameter 'item' we are able to access corresponding values.</p>
<p>In our example on line 7 we access the key 'name' and log its values from all the objects to a console (item => console.log(item.name)). The result will be the names from all the object logged to a console one after the other.</p>
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/4e47iomrrivrzkqn3p4n.jpeg)

<h1>Fetch</h1>
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/irow2qig5lbsnagput5u.jpeg)
<p>When we send a request to a server using fetch() method we receive a JSON object and save it in a variable 'res'. You can give a variable any name you like. Make sure that you don't put semicolon after fetch().</p>
<p>On the next line we append a .then() method to our fetch() request. .then() method grasps data returned on the previous step, does something new with it and returns the result. To handle this returned data we pass in a callback function inside .then(). In our first .then() we convert JSON object into JavaScript object (res => res.json()).</p>
<p>After that we append another .then(). The second .then() grasps the data returned by the previous .then(), which is an array of JavaScript objects, iterates over all objects and logs all the name to a console.</p>
<p>The final result in a console will be absolutely identical to what we saw when we sent a AJAX/XMLHttpRequest above.</p>