bg1420
bg1420

Reputation: 321

JSON parsing using jQuery in API html link

I am trying to write a Jquery to Parse product information from an api.

<div id="placeholder"></div> script....

$.getJSON(data);
var output = "<ul>";
for (var i in data.products) {
output += "<li>" + data.products[i].sku + " " + data.products[i].name + "--" + 
data.products[i].salePrice + "</li>";
}
output += "</ul>";
document.getElementById("placeholder").innerHTML = output;`

when I give var data = {"products":[{"id_item":12324,"id_name":"canon"} it works but if I pass a link var data = "http://api.remix.bestbuy.com/v1/products(manufacturer=canon&salePrice%3C1000)?format=json&show=sku,name,salePrice&apiKey=<API_KEY> it does not work. I am a beginner and trying to learn JQUERY. Any kind of help will be appreciated...Is there any code debugger for Jquery... Thanks in advance...

Upvotes: 0

Views: 195

Answers (1)

dev
dev

Reputation: 4009

If you're setting the variable data for a URL and using that in the getJSON function then your data.products etc.. is not actually referencing anything because data is still pointing at your URL.

Take a look at the getJSON API Documentation but you'll need to do something like the below.

var url = "http://api.remix.bestbu....."
$.getJSON(url, function(data){
   var output = "<ul>";
   for (var i in data.products) {
      etc.....
});

EDIT

For JSONP you will need to specify the data type using the $.ajax function. I'm not aware of a way doing it with $.getJSON, someone correct me please if I;m wrong.

$.ajax({
   type:"GET",
   dataType: "JSONP",
   url: url,
   success:function(data){
      // .... Your function with the data. 
   }
});

Upvotes: 1

Related Questions