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");
}
})
});