In this article, weā€™ll be looking at how we can go about retrieving data from a server. Modern websites and applications need to be able to do this seamlessly ā€” that is, update sections of a web page without needing to reload the entire page. Weā€™ll be looking at two technologies that make this possible:Ā XMLHttpRequestĀ and theĀ Fetch API.

This article continues on from my previous:Ā Understanding APIs. If youā€™re new to working with APIs, be sure to check it out first!

The old way...

Page loading on the web using old methods was quite simple ā€” you would send a request for a website to a server, and if nothing went wrong, any assets that made up the page would be downloaded and displayed on your computer or device.

This was the way websites worked for years. However it had a number of flaws.. whenever you wanted to update any part of the page, for example, to display a new set of products or submit a form, the entire page had to load again. This practice is very resource-heavy and results in a poor UX, especially as pages grow and become more complex.

The solution: AJAX

This problem was resolved as technologies were created that allowed web pages to request small chunks of data (such as HTML, XML, JSON, or plain text) and display them only when needed.

This is achieved by using APIs likeĀ XMLHttpRequestĀ or the Fetch API. These technologies allow web pages to directly handle making HTTP requests for specific resources available on a server and formatting the resulting data as needed before it is displayed.

This technique is what is calledĀ ā€œAjaxā€, orĀ AsynchronousĀ JavaScriptĀ andĀ XML. Originally it mainly usedĀ XMLHttpRequestĀ to request XML data (hence the ā€œand XMLā€). However, these days you'd be more likely to useĀ XMLHttpRequestĀ or Fetch to request JSON ā€” but the result is still the same so the term "Ajax" stuck.

Conceptually, Ajax involves using a web API as a proxy to more intelligently request data, rather than just having the browser reload the entire page.

You can see it in action in just about every major website. When you play a new video on YouTube for example, youā€™ll see the navigation, header and footer sections donā€™t refresh ā€” just the section containing the video.

Why is this important?

  • Page updates are much faster & youā€™ll spend less time wait for the page to refresh, so the site feels faster and more responsive (page speed is also very important for Google rankings and SEO!).
  • Less data is downloaded on each update, meaning less wasted bandwidth. Whilst not a huge concern on desktops, itā€™s a major issue on mobile devices especially in developing countries that donā€™t have fast Internet.

Creating an Ajax request

In this section, weā€™re going to first use anĀ XMLHttpRequestĀ and then Fetch to perform an Ajax request.

XMLHttpRequest

AnĀ XMLHttpRequestĀ (often abbreviated toĀ XHR) is actually quite an old technology now ā€” it was invented in the ā€˜90s by Microsoft, and has been in use ever since.

To create an XHR request, you first need to create a new request object using theĀ XMLHttpRequest()Ā constructor. You can call this object anything you like, in our demo weā€™ll call itĀ xhr. Like so:

let xhr = new XMLHttpRequest(); 

The next step is to initialize it using theĀ open()Ā method. Here we specify what HTTP request method to use to request the resource from the network, as well as the URL where itā€™s located. Here weā€™ll use theĀ GETmethod and set the URL to aĀ urlĀ variable (which should store the address as a string).

We add this below the previous line:

xhr.open('GET', url);

Next, weā€™ll set up the type of response we are expecting asĀ text. This isn't strictly necessary as XHR returns text by default ā€” but it is a good idea to get into this habit as youā€™ll no doubt be fetching other types of data in the future! Now we add:

xhr.responseType = 'text';

Whenever we fetch a resource from a network weā€™re performing an asynchronous operation. This means you have to wait for that operation to complete (while the resource is returned from the network) before you can do anything with that response, otherwise, an error will be thrown. With XHR we can handle this using theĀ onloadĀ event handler ā€” this runs when theĀ loadĀ event fires (after the response has returned). The response data will be available in theĀ responseĀ property of the XHR request object.

Next, we create our function to handle theĀ onloadĀ event & (in this case ) display ourĀ xhr.status&Ā xhr.responseĀ values:

xhr.onload = function() {
  alert(`Loaded: ${xhr.status} ${xhr.response}`);
};

And finally, we need aĀ send()Ā method to run the request:

xhr.send();

Fetch

The Fetch API is a modern alternative that can serve as a replacement for XHR. It was built to make asynchronous HTTP requests easy to do in JavaScript.

Letā€™s see a basic example of the syntax:

fetch(url) // Call the fetch function passing the url of the API as a parameter
.then(function() {
    // Your code for handling the data you get from the API
})
.catch(function() {
    // This is where you run code if the server returns any errors
});

All we are doing here is fetching the URL that we want, by default fetch uses the GET method ā€” which is what we want in this case!

Letā€™s see some more fleshed out sample code:

fetch('http://example.com/songs.json')
  .then(function(response) {
    return response.json();
  })
  .then(function(myJson) {
    console.log(JSON.stringify(myJson));
  });

Here we are fetching a JSON file from the URL and then printing it to the console. The simplest use ofĀ fetch()Ā takes one argument ā€” the path to the resource you want to fetch ā€” and returns a promise containing the response (in the form of a JavaScript object).

To extract the JSON body content from the response, we use theĀ JSON.stringify(myJson)method. Then we can transform the response object into an object that we can interact with.

Working with promises

Like many modern JavaScript APIs, Fetch uses promises. Though they are a bit confusing at first, donā€™t worry too much about it for now. Youā€™ll get used to them!

Letā€™s look at the promise structure as it relates to our Fetch request:

fetch('http://example.com/songs.json')
  .then(function(response) {
    return response.json();
  })
  .then(function(myJson) {
    console.log(JSON.stringify(myJson));
});

The first line is saying ā€œfetch the resource located our URLā€ (fetch('http://example.com/songs.json')) and "then run the specified function when the promise resolves" (.then(function() { ... })). "Resolve" means "finish performing the specified operation at some point in the future".

The specified operation, in this case, is to fetch a resource from our specified URL (using an HTTP request) and return the response for us to do something with.

Effectively, the function passed intoĀ then()Ā is a chunk of code that won't run immediately. Instead, itā€™ll run at some point in the future when the response has been returned.

So which technique should you use?

XHR has been around for a long time now and has very good cross-browser support. Fetch and Promises, on the other hand, are newer additions to the web platform, although theyā€™re supported well across the browser landscape, with the exception of Internet Explorer.

If you need to support older browsers, then an XHR solution might be preferable. If however you are working on a more progressive project and arenā€™t as worried about older browsers, then Fetch would be a good choice.

Wrapping up

And thatā€™s it! We looked at how to start working with AJAX using techniques such as XHR and Fetch to retrieve data from a server. And we looked at basic examples of each! Hopefully, now you have a grasp of each & can start incorporating async operations into your code!

Related Posts:


Tim profile image

A little about me..

Hey, Iā€™m Tim! šŸ‘‹

Iā€™m a freelance business owner, web developer & author. I teach both new and experienced freelancers how to build a sustainable and successful freelancing business. Check out myĀ Complete Guide to Freelancing if you'd like to find out more.

While you're here, you can browse through my blogs where I post freelancing tips, code tutorials, design inspiration, useful tools & resources, and much more! You can also join the newsletter, or find me on X.

Thanks for reading! šŸŽ‰