我对通过 ajax 加载的 json 编码信息有疑问。我将多 JSON 传递给 ajax 以显示字段值。
如何使用查询 ajax 从 json 中获取字段值。
阿贾克斯代码:
....success:function(data){
var TotalBuyPrice = 0;
var TotalItem = 0;
$.each(data, function(c,cart){
//Condition follow 1
var InStockQty =cart.products_qty;
alert(InStockQty);
//And also follow 2
var name =cart["withoutdiscount"][0]["products_name"];
alert(name);
});
}...
PHP代码:
这些是我对 json 响应的步骤。通过使用数组收集结果
$response = array();
$response['withoutdiscount'] = $withoutdiscount;
$response['withdiscount'] = $withdiscount;
echo $_GET['jsoncallback'] . '(' . json_encode($response). ');';
json回调:
({"withoutdiscount":[{"products_id":"1","products_name":"Lumia"}],
"withdiscount":[{"products_id":"2","discount_qty":"8"},
{"products_id":"3","discount_qty":"1"}
]
});