Working with an API using JSON and Jquery
-
I’ve got a question about calling and displaying an API from Shopping.com. I’ve got it working on a non-wordpress site and am trying to translate it over. The code is in the <head> section on my other site and the productID is specific for each page it is on. I don’t think that would work here since every page shares the same <head>. Here’s the code:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script> $.ajax({ dataType: "json", url: 'http://api.ebaycommercenetwork.com/publisher/3.0/json/GeneralSearch?apiKey=xxx&visitorUserAgent&visitorIPAddress&trackingId=xxx&productId=146798281', context: document.body }).done(function(data) { $("<h5>"+data.searchHistory.productSelection[0].name+"</h5>").appendTo("#shopping-com-name"); var product_item = data.categories.category[0].items.item[0].product.offers.offer; $.each(product_item, function( index, value ) { var product_link ='<a target="_blank" href="'+value.offerURL+'"><img src="http://lcdtvbuyingguide.com/graphics/shop-button.png"></a>' ; var store_logo='<a target="_blank" href="'+value.offerURL+'"><img src="'+value.store.logo.sourceURL+'"></a>' ; var product_price='<a target="_blank" href="'+value.offerURL+'">$'+value.basePrice.value+'</a>'; $("<tr><td>"+product_link+"</td><td>"+store_logo+"</td><td>"+product_price+"</td></tr>").appendTo("#shopping-com-list tbody"); }); }); </script> </head> <body> <span id="shopping-com-name"></span> <table id="shopping-com-list"> <thead> <tr> <th></th> <th>Stores</th> <th>Prices</th> </tr> </thead> <tbody> </tbody> </table> </body>The table then goes in the <body> of the page where I want it to render. (API key and tracking ID x’ed out on purpose)
Even if I put this in the <head> it doesn’t work. It seems like I need to make a different approach due to the way wordpress functions. Anybody got an idea to steer me in the right direction?
The topic ‘Working with an API using JSON and Jquery’ is closed to new replies.