Tiny Frontend Logo
Posts 0129

On This Page

Data Fetching from the Server

Do you know what AJAX is? It seems to be an "old" concept today, it allows us to use JavaScript to send asynchronous data requests to the server and update the rendering of the page locally.

I still remember the excitement when I first knew this concept, I knew that a webpage was no longer just a piece of boring HTML code.

Today, we will focus on one of the most important features in a web application: Data Fetching from the Server.

Data Fetch

The Fetch Method

The fetch() method is a global method that provides a straightforward, logical method to fetch resources asynchronously over the network.

It gives back a Promise that resolves into a Response object, which signifies the outcome of the request.

Syntax

const fetchPromise = fetch(url, options);

fetchPromise.then(res => {
  // success response
  // ...
}).catch(ex => {
  // exceptions
  // ...
});
  • url: defines the resource that you wish to fetch.
  • options: optional, an object containing any custom settings you want to apply to the request.
    • e.g. method, body, headers, etc

Basic Example

Let's try to fetch data using the default GET method.

fetch('https://example.com/books.json')
  .then(response => {
    return response.json();
  })
  .then(response => {
    console.log(response);
  })
  .catch(ex => {
    console.error(ex);
  });
  1. We use the fetch() method to make an HTTP request to the specified URL ("http://example.com/books.json").
  2. The fetch() method returns a Promise that resolves with a Response object.
  3. To extract the JSON response body, we use the json() method on the Response object.
Subscribe to read the full content

* Free subscribe to enjoy full content access.
* You can cancel anytime with 1-click.

Read Next

0128

JavaScript Currying

The first time I heard the term Currying was during a job interview preparation. Fortunately, the interviewer did not ask me any questions about it.

JavaScript Currying
0127

FormData in JavaScript

The most common way of sending data to the server in a network request is by using a JSON object. But, in some cases, JSON maybe insufficient to transmit the data.

FormData in JavaScript
0126

HTTP Status Code for SEO

When a browser sends a request, the server responds with a corresponding HTTP status code, whether the request can be fulfilled or not.

HTTP Status Code for SEO
0125

Data Transfer with WebSocket

You're probably already familiar with using the HTTP protocol to retrieve data from servers. However, in addition to HTTP, browsers also provide another networking protocol to meet more advanced communication needs: WebSocket.

Data Transfer with WebSocket
0124

Permissions on a Webpage

Your website cannot readily use some services like camera, microphone, location, etc. on the user device. For security reasons, the websites need to specifically request for permissions from the user to use these services. In this post, let's learn how to handle these permissions on a webpage.

Permissions on a Webpage
0123

Network Requests in JavaScript

One of the crucial and fundamental component of a website is to make Network Requests to fetch some data. There are a lot of libraries available now, like axios, that make this task very easy to implement for the developers. But, if you've ever wondered what goes on underneath those libraries or if you want to achieve this using pure JavaScript, keep reading.

Network Requests in JavaScript