0

I am having trouble parsing from this api. Can you see what I have done?

The PHP Curl Call:

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
$options = array(CURLOPT_URL => 'http://api.remix.bestbuy.com/v1/products(salePrice<='.$dollars.'&type=Music)?apiKey=gzunxsecretsdssf444&format=json&show=name,salePrice,shortDescription,image',
                 CURLOPT_HEADER => false,
                 CURLOPT_RETURNTRANSFER => 1
                );

curl_setopt_array($ch, $options);

// grab URL and pass it to the browser
echo curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch); 

The js:

$.ajax({
           url: "./file.php",
           type: 'GET',
            dataType: 'JSON',
           success: function (data) {
                console.log("Success");
                console.log(data);
                for(var i=0;i<=data.products.length;i++)
                {
                    var thumb=data.products[i].image;
                    $('<div class="product" style="background-color:green"><a href=""><img src="'+ thumb +'" alt""/></a></div>').appendTo('#find_stuff_div');
                }
            }
           });

var thumb=data.products[i].image; is where I am having trouble. The object itself logs (apparently as json) but var thumb returns "Uncaught TypeError: Cannot read property 'image' of undefined ". Any ideas?

Console log console

4

2 回答 2

0

对不起,你的 for 循环是错误的

for(var i=0;i<data.products.length;i++)

数组索引从0to开始,所以你的条件不length - 1应该是i<data.products.lengthi<=data.products.length

于 2013-03-21T03:18:24.260 回答
0

您需要调用JSON.parse()返回的数据才能将其用作对象。

于 2013-03-21T02:56:26.587 回答