javascriptapiclient-side-scripting

Is it possible to get 3rd party API using client-side script only


Let say I have access to check shipping cost by using 3rd party API with API-KEY

Is it possible to get/request the data using client-side script only (like javascript or mustache or else)?

Here is the documentation from the provider


Solution

  • You could do this with AJAX.

    If you had to send a GET to https://api.rajaongkir.com/starter/province , the AJAX request will look as follows:

    $.ajax({
             url: "https://api.rajaongkir.com/starter/province",
             type: "GET",
             beforeSend: function(xhr){xhr.setRequestHeader('key', 'API Key');},
             success: function() { //do something here post the GET }
          });
    

    UPDATE:

    You will need to use jQuery for $.ajax. You can do that by including the jQuery library in your project. In your <head>, add the following:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

    This should enable you to use AJAX for sending asynchronous network requests.

    I have written one request where I send a GET to the https://swapi.co/api/people/1/ and display the name in div

    https://jsfiddle.net/raj7desai/mtdnzoh0/