If you have any question left, please drop a note with the discussion link of this page.

Javascript Callback Handler

From Wiki.zanox.com

(Redirected from Callback Handler)
Jump to: navigation, search

The zanox API cannot be called via AJAX due to the same origin policy.

Instead, the API offers a callback parameter for use with a dynamic script tag.

Following are two examples of a product search for "auto" in the advertiser programs with ID 660, 10, 20, and 30.

Usage of 'callback=function' with static script tag

<script>
  function myCallbackHandler(jsonData){
  // Do something with the jsonData returned by the API query.
  }
</script>
<script src="http://api.zanox.com/json/2009-07-01/products?q=auto&programs=660,10,20,30&connectid=580599047DF8F5311043&callback=myCallbackHandler"> </script>
 

Usage of 'callback=function' with dynamic script tag

<script>
  function callAPI(url){
    var script = document.createElement('script');
    script.src = url; // Sets the script's source to the Rest/JSON API query.
    script.type = "text/javascript";
 
    document.getElementsByTagName('head')[0].appendChild(script); 
    // The browser performs a request to the zanox API. 
    // As soon as the script tag's source is loaded, the callback function is called.
    }
 
    function myCallbackHandler(jsonData){
    // Do something with the jsonData returned by the API query.
    }
 
  var url = 'http://api.zanox.com/json/2009-07-01/products?q=auto&programs=660,10,20,30&connectid=580599047DF8F5311043&callback=myCallbackHandler';
  callAPI(url); // This call could, for instance, be moved to an HTML element's "onclick" event
</script>

Both examples result in the dynamic creation and execution of the following:

 
<script>
myCallbackHandler({"page":0,"items":10,"total":85622,"query":"auto","productItems":{ ... });
</script>
 
Personal tools