Simple AJAX call with Fetch API

Nowadays a lot of people make AJAX calls with JQuery, I know people that just download JQuery just to make this kind of calls to RestAPIs(I was doing it not much time ago) and is not really necessary download a whole library to make this kind of calls especially if the page download time is a important issue.

If is not with JQuery what alternative do I have?

Well, since 2015 their is a light and native(for browsers) alternative called Fetch API.

Fetch API allows us to make AJAX calls using another feature call Promise, for a better definition I will quote to Sandeep Panda:

Fetch is a new, simple and standardised API that aims to unify fetching across the web and replace XMLHttpRequest. It has a polyfill for older browsers and should be used in modern web apps.

How it works?

Here is a simple example:
JSON Example Data

{
"user": {
"name": "Wilhelm",
"email": "email@domain.com"
}
}

HTML and JS code

References:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s