How to make HTTP calls using simple Javascript

XMLHttpRequest is a built-in browser object that allows to make HTTP requests in JavaScript.

Despite of having the word “XML” in its name, it can operate on any data, not only in XML format. We can upload/download files, track progress and much more.

Right now, there’s another, more modern method fetch, that will look later.

You can follow the below steps to invoke http calls or do a copy and paste to make it work :)

const xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() { //debugger; //if(xhr.status == 201) { alert("Thank you for contacting me. I will get back to you very soon."); //} }

const json = { "fullName":document.getElementById("name").value, "emailAddress": document.getElementById("emailaddress").value, "message": document.getElementById("message").value, "subcriber":true

};

xhr.open('POST', 'localhost:8080/sitename/stories');

xhr.setRequestHeader('Content-Type', 'application/json');

xhr.setRequestHeader('Authorization', 'Basic c3VwZXJtYW4dd6c3VwZXI=');// I have used Basic authentcation enabled REST call here, Like here you you use any kind of authentication, credententials etc..

xhr.send(JSON.stringify(json));