Tuesday 1 March 2016

How to call restful api from javascript or hit rest api URL from javascript.

What is REST api?
REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol and in virtually all cases, the HTTP protocol is used. REST is an architecture style for designing networked applications. 
 Use following code to hit rest api url from javascript and it returns  in json format.
 var myJSONData = {"key1":"value1","key2":"value2","key3":"value3"};

    $.ajax({
            type: 'POST',
            url: 'https://example.com/ttm/subscribe-service',//rest api url
            data: myJSONData,
            dataType: 'application/json',
            success: function(data) { 
             alert("request posted to the sever successfully");
             $('#message').html('<h1>POSTED</h1>');     
          } // Success Function end
          });   // Ajax Call end