[英]JSON AJAX request returns undefined when using square brackets
我在用一些用方括号括起来的JSON数据发出AJAX请求时遇到了麻烦-该值返回undefined
。
我已经测试了第二个JSON文件,该文件可以在没有方括号的情况下正常工作,但不能与方括号一起使用,因此我知道这绝对是问题所在。
问题是我该在哪里进行更改:在JSON或AJAX请求中?
这是创建此JSON文件的 PHP代码段。
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);
这是我的AJAX请求:
$('#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");
}
})
});
foreach ($product_urls as $i => $product_url) { $result[] =...
$result
是一个行数组,所以success: function(result)
result
是一个行数组,而不是单个行。
所以这将显示一个URL
var product_url = result[0]['product_url'];
我猜你想迭代它们吗?
$('#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);
}
如何尝试使用此:
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]
));
而不是$ result []?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.