Welcome‎ > ‎Version 2‎ > ‎Reference Info‎ > ‎

JSONP

The API supports JSONP for syndication methods only.  JSONP enables API users to wrap the JSON API output in a function call to operate on data from the (external) groupcommerce domain.

To wrap the JSON API output in a callback function, simply set the querystring variable 'callback' to the name of the function.  The output becomes: {function name} ( {JSON data from api} ); 

For example:
Request (note callback function is 'testFunction'):

http://apitest.groupcommerce.com/api/v2/syndication/segments?apiKey=43c5c674-620c-4f2b-bdcf-3082c99e8186&callback=testFunction

 
Returns:
testFunction (
{
 "version":"2.0",
 "success":true,
 "lastPublished":"\/Date(1318344806375)\/",
 "data":[
  {
   "key":"ny",
   "latitude":40.47,
   "longitude":-73.58,
   "name":"New York",
   "timezone":"Eastern Standard Time",
   "viewOrder":0
  },
  {
   "key":"sea",
   "latitude":47.37,
   "longitude":-122.2,
   "name":"Seattle",
   "timezone":"Pacific Standard Time",
   "viewOrder":0
  }
 ],
 "metaData":{
  "pageCount":1,
  "pageNumber":1,
  "pageSize":4,
  "totalItems":4}
 }
);

Simple web page example using JSONP - displays the first image and its size for every current offer for a specific publisher

<html>
    <
body>
    <
h1>Get All Images</h1>

    <
div id="imagedemo"></div>

    <
script type="text/javascript">
        function offerCallback(response)
        {
            var imgs = [];
            for( var i = 0 ; i < response.data.length ; i++)
            {
                 var img = response.data[i].images[0];
                 imgs.push("<p>Image size:" + img.size + "</p><div>" + "<img src=" + img.url + " <\/><\/div>");
            }


            document.getElementById("imagedemo").innerHTML = imgs.join('');
        }
     </
script>

     <script src="http://apitest.groupcommerce.com/api/v2/syndication/offers/current/ny?callback=offerCallback&apiKey=43c5c674-620c-4f2b-bdcf-3082c99e8186"></script>
     </
body>
</
html>


Another example - displays all active segments for a specific publisher:
<html>
 <body>
 <h1>Segments</h1>


 <div id="segments"></div>


 <script type="text/javascript">
  function segmentCallback(segmentJSON)
  {
   var segments = [];


   for( var i = 0 ; i < segmentJSON.data.length ; i++)
   {
    segments.push("<div>" + segmentJSON.data[i].name + "</div>");
   }


   document.getElementById("segments").innerHTML = segments.join('');


  }
 </script>


 <script src="http://apitest.groupcommerce.com/api/v2/syndication/segments?callback=segmentCallback&apiKey=43c5c674-620c-4f2b-bdcf-3082c99e8186"></script>


 </body>
</html>

Comments