Ways to make Http requests from javascript


Ajax : - Traditional way to make an asynchronous http request. 
const http = new XMLHttpRequest();
const url = 'https://jsonplaceholder.typicode.com/todos/1';
http.open('GET', url);
http.send();

http.onreadystatechange = (data) => {console.log(data)}; 

JQuery Library : - You can insert jquey library script and use, $.ajax, $get, $.post methods.

Fetch Api :  Powerful web api returns "Promise" defined in ES6.
const url = 'https://jsonplaceholder.typicode.com/todos/1';
const httpPromise = fetch(url);
httpPromise
.then(data => { return data.json() })
.then( response => { console.log(response)}); 


Axios : - Open source library for making http requests

Angular HttpClient : -

Reference :

  1. https://medium.freecodecamp.org/here-is-the-most-popular-ways-to-make-an-http-request-in-javascript-954ce8c95aaa

Comments