Welcome‎ > ‎Version 3‎ > ‎Reference Info‎ > ‎

JSONP

Version 3 of the api supports JSONP for broadcast 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'):
GET http://api.groupcommerce.com/api/v3/publisher/broadcast/segments?ApiKey=eeb6b959-b854-4ad1-b563-e801c83cf81d&callback=testFunction
 
Returns:
testFunction({
    "errors": [
       
    ],
    "lastPublished": "\/Date(1325802591016)\/",
    "success": true,
    "version": "3",
    "metaData": {
        "pageCount": 1,
        "pageNumber": 1,
        "pageSize": 2,
        "totalItems": 2
    },
    "segments": [
        {
           
/* see Broadcast Segment */
        },
        {
           
/* see Broadcast Segment */
        }
    ]
});

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.offers.length ; i++)
            {
                 var img = response.
offers[i].images[0];
                 imgs.push("<p>Image size:" + img.height + "x" + img.width + "</p><div>" + "<img src=" + img.url + " <\/><\/div>");
            }


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

     <script src="http://api.groupcommerce.com/api/v3/publisher/broadcast/offers?ApiKey=eeb6b959-b854-4ad1-b563-e801c83cf81d&callback=offerCallback"></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.segments.length ; i++)
   {
    segments.push("<div>" + segmentJSON.segments[i].name + "</div>");
   }


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


  }
 </script>


 <script src="http://api.groupcommerce.com/api/v3/publisher/broadcast/segments?ApiKey=eeb6b959-b854-4ad1-b563-e801c83cf81d&callback=segmentCallback"></script>


 </body>
</html>

Comments