Amazon Cognito

Es un servicio que te ayuda a mantener un registro de usuarios, te otorga métodos tanto para logeo como para validación de usuarios mediante email o número celular. También te permite agregar…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Different ways of interacting with servers in JavaScript

Most of the web pages and web application we see now a days are rarely static pages. These dynamic web pages and application request contents from servers and updates the document, without having to reload the whole page; this is exactly what AJAX let us do. AJAX stands for Asynchronous JavaScript and XML. It originally used XML for formatting the data that’s why the name stuck, but now a days its more common to see JSON(JavaScrip Object Notation) for data processing. There are more than one way of retrieving and sending data to the servers and today we will explore couple of these methods.

Using XHR(XML Http Request) Object:

Even though the name suggest XHR would work only with the XML, we can use XHR to retrieve JSON data as well. Let’s look at a simple XHR request.

In above example, we first create a XHR object and pass any url you would like to send your request to. Then we open that request using .open() method and once the request is open we can send the request over using .send() method. Finally, we will print out the text version of the data using .responseText, but before that we check if the response is complete by checking the “readystatechange” value.

Using Fetch API:

The fetch() method, takes care of opening the connection and sending the request without us having to do anything. First we pass the url to the fetch() method and then we convert the response to json using .json() and finally we take that data and just print it out. In the above example, we have that specified what kind of method we are using to use for the given fetch request, by default it is always ‘GET’. If you want to make other requests, we can pass them as second argument to fetch along with other informations like headers. So, the data object that we would pass as an argument would look something like this.

We can see, sending a ‘POST’ request is also very simple to follow and require less amount of code using fetch. Its simplicity is one of the reasons why using fetch API is much more popular these days than XHR.

That’s all for this edition of my blog, until next time happy coding.

Add a comment

Related posts:

Netflix for paywalled articles

there should be a netflix/spotify for paywalled articles agnostic of media outlet, eg. medium/economist/FT etc. All these media outlets have their own paywall, and its reasonable and fair that…

When the Noise Falls Away

The heat rises with the day and my legs are like pistons beneath me, propelling me forward. We stop for sips of water in patches of shade cast by small croppings of rock or the tiny barren trees and…

My amal journey

This was the best decision of my life that amal choose me to be part of them. There were so many activities in amal that help me grow my personality a lot. All those activities help me push my…