I have a problem with json encoded information which loaded via ajax.I pass the multi JSON to the ajax for display the field value.
How can i fetch the field value from the json using query ajax.
ajax code :
....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);
});
}...
The PHP code :
These are my steps following for json response.By using array collect the result
$response = array();
$response['withoutdiscount'] = $withoutdiscount;
$response['withdiscount'] = $withdiscount;
echo $_GET['jsoncallback'] . '(' . json_encode($response). ');';
jsoncallback:
({"withoutdiscount":[{"products_id":"1","products_name":"Lumia"}],
"withdiscount":[{"products_id":"2","discount_qty":"8"},
{"products_id":"3","discount_qty":"1"}
]
});
I Solve the problem like this: Using PHP file get one response like this:
$response = array();
$response['withoutdiscount'] = $withoutdiscount;
$response['withdiscount'] = $withdiscount;
echo $_GET['jsoncallback'] . '(' . json_encode($response). ');';
jsoncallback: //JSON response
({"withoutdiscount":[{"products_id":"1","products_name":"Lumia"}],
"withdiscount":[{"products_id":"2","discount_qty":"8"},
{"products_id":"3","discount_qty":"1"}
]
});
using ajax function call the json response like this:
function querySuccess(tx,results) {
var jsonString = JSON.stringify(results);
$.ajax({
url:'getcart.php',
data: {data : jsonString},
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success:function(data){
var withdiscount=data.withdiscount;
var withoutdiscount=data.withoutdiscount;
if(withdiscount!='')
{
$.each(withdiscount, function(c,cart){
var discount_qty =cart.discount_qty;
alert(discount_qty);
);
}
if(withoutdiscount!='')
{
$.each(withoutdiscount, function(c,cart){
var products_name =cart.products_name;
alert(products_name);
});
}
}
});
}
This is working with me.
NOTE: There is only 2 JSON response is pass so i given directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.