[英]Use jquery's each() to loop through php array on success return
How do I loop using JQuery's each()
to get the data [sku]
and [imagePath]
that is returned to my success function from PHP array using AJAX's post? 如何使用JQuery的
each()
循环获取使用AJAX的帖子从PHP数组返回到成功函数的数据[sku]
和[imagePath]
?
Below is the array. 下面是数组。
Array
(
[id] => 195
[sku] => Lemonpunnet
[variantName] => Lemon punnet
[qty] => 500
[productId] => SGG-PRD-3
[prodVariantStatus] => 1
[visible] => 1
[pricing] => Array
(
[price] => 660
)
[modifiedDate] => 1448294444
[createDate] => 1448294444
[productVariantImages] => Array
(
[0] => Array
(
[id] => 268
[imageName] => lemon.png
[imagePath] => http://web-server-s3.s3.amazonaws.com/9/catalogue/lemon.png
[visible] => 1
[featured] =>
[modifiedDate] => 1448294444
[createDate] => 1448294444
)
)
)
) )
And the following is what I currently have but it is not returning any data. 以下是我目前拥有的内容,但未返回任何数据。
$('#selectVariant').change(function () {
var sku = $('#selectVariant :selected').val();
var sessionId="<?php echo $sessionId; ?>";
var dataString='sku='+ sku +'&sessionId='+sessionId;
$.ajax({
type:"post",
url: "<?php echo $base_url; ?>ajax-helper/search_variant.php",
data:dataString,
cache:false,
dataType: "JSON",
success: function(data){
$.each(data.variants,function(key,value){
alert(key+":"+value);
});
}
});
return false;
Run .each
on the data
response. 对
data
响应运行.each
。
Or if you want an individual element of the data
object, address it directly. 或者,如果您想要
data
对象的单个元素,请直接对其进行寻址。
Example: 例:
success: function(data){
$.each(data,function(key,value){
console.log(key+":"+value);
});
// outside of the each
console.log(data.sku);
$.each(data.productVariantImages, function(){
console.log(this.imagePath);
})
}
If your data is JSON string: 如果您的数据是JSON字符串:
alert(data['sku']);
$.each(data['productVariantImages'],function(key,value){
$.each(value,function(key2,value2){
if (key2 == 'imagePath') {
alert(value2);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.