close

How to call a REST web service API from JavaScript?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to call a REST web service API from JavaScript? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to call a REST web service API from JavaScript ?

  1. How to call a REST web service API from JavaScript ?

    to call a REST web service API from JavaScript
    I think add if (this.readyState == 4 && this.status == 200) to wait is better:
    var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { // Typical action to be performed when the document is ready: var response = xhttp.responseText; console.log("ok"+response); } }; xhttp.open("GET", "your url", true); xhttp.send();

  2. How to call a REST web service API from JavaScript ?

    to call a REST web service API from JavaScript
    I think add if (this.readyState == 4 && this.status == 200) to wait is better:
    var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { // Typical action to be performed when the document is ready: var response = xhttp.responseText; console.log("ok"+response); } }; xhttp.open("GET", "your url", true); xhttp.send();

Method 1

 $("button").on("click",function(){
      //console.log("hii");
      $.ajax({
        headers:{  
           "key":"your key",
     "Accept":"application/json",//depends on your api
      "Content-type":"application/x-www-form-urlencoded"//depends on your api
        },   url:"url you need",
        success:function(response){
          var r=JSON.parse(response);
          $("#main").html(r.base);
        }
      });
});

Method 2


I think add if (this.readyState == 4 && this.status == 200) to wait is better:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       // Typical action to be performed when the document is ready:
        var response = xhttp.responseText;
        console.log("ok"+response);
    }
};
xhttp.open("GET", "your url", true);

xhttp.send();

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment