1

I'm having trouble making an AJAX request with some JSON data that's enclosed in square brackets - the value returns undefined.

I've tested a second JSON file that works fine without square brackets but not with them, so I know that's definitely the issue.

The question is where do I make the change: in the JSON or in the AJAX request?

Here's my PHP snippet which creates this JSON file.

foreach ($product_urls as $i => $product_url) {
    $result[] = array(
        'product_url' => $product_url,
        'shop_name' => $shop_name,
        'photo_url' => $photo_url[$i],
        'was_price' => $was_price[$i],
        'now_price' => $now_price[$i]
    );
}
echo json_encode($result);

Here's my AJAX request:

$('#container').html('<h3 class="feeds-loader text-muted" style="text-align: center;">Loading...</h3>');
    $.ajax({
        url: 'http://www.comfyshoulderrest.com/shopaholic/rss/asos_f_uk.php?id=1',
        type: 'GET',
        dataType: 'json',
        success: function(result) {
            $('#container').html('');
            var product_url = result['product_url'];
            var shop_name = result['shop_name'];
            var photo_url = result['photo_url'];
            var was_price = result['was_price'];
            var now_price = result['now_price'];

            alert(product_url);
        },
        error: function() {
           alert("error");
        }
    })
});
jimmyp.smith
  • 178
  • 6
Sebastian
  • 3,548
  • 18
  • 60
  • 95
  • What data enclosed in square brackets? Can you give an example of what works and what does not? – dkasipovic Mar 26 '14 at 22:58
  • [This](http://www.comfyshoulderrest.com/shopaholic/rss/test.php) doesn't work but it does without square brackets. The JSON file linked in the question with square brackets doesn't work. – Sebastian Mar 26 '14 at 22:59
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Mar 26 '14 at 23:01

2 Answers2

2

foreach ($product_urls as $i => $product_url) { $result[] =...

$result is an array of rows, so in success: function(result) result is an array of rows, not a single row.

So this would show a URL

var product_url = result[0]['product_url'];

I'm guessing you want to iterate them?

$('#container').html('');
for (i in result){
        var product_url = result[i]['product_url'];
        var shop_name = result[i]['shop_name'];
        var photo_url = result[i]['photo_url'];
        var was_price = result[i]['was_price'];
        var now_price = result[i]['now_price'];

        alert(product_url);    
}
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
0

How about try to use this:

array_push($result, array(
            'product_url' => $product_url,
            'shop_name' => $shop_name,
            'photo_url' => $photo_url[$i],
            'was_price' => $was_price[$i],
            'now_price' => $now_price[$i]
        ));

Instead of $result[]?

Nikko R.
  • 178
  • 1
  • 11