[英]Return multiple values from jquery ajax call to php
I am trying to return multiple values from a php process.我正在尝试从 php 进程返回多个值。
Here is the jQuery function这是jQuery函数
$.ajax({
url: 'shopping_cart_add.php',
data: 'item_id='+subscription_id+'&item_name='+subscription_name+'&item_price='+subscription_price,
type: 'POST',
dataType: 'json',
success: function(response, statusText) {
var qty = response.item_quantity;
$("#shopping-cart-quantity").html(qty);
}
});
The above seems to work except I can't retrieve the specific field values from the returned JSON.以上似乎有效,但我无法从返回的 JSON 中检索特定字段值。
When I try this...当我尝试这个...
var qty = response.item_quantity;
$("#shopping-cart-quantity").html(qty);
Nothing happens.没发生什么事。
If I change...如果我改变...
$("#shopping-cart-quantity").html(qty);
to到
$("#shopping-cart-quantity").html(response);
I get the following...我得到以下...
{ 'account_id': '1', 'item_id' : 'cce3d2a017f6f1870ce8480a32506bed', 'item_name' : 'CE', 'item_quantity' : '1', 'item_price' : '1' }
Please make sure that you are using json_encode() for returning result array请确保您使用 json_encode() 返回结果数组
/*** PHP ***/
echo json_encode($resultArr); exit ;
And in AJAX try with eval() to access response text value .在 AJAX 中尝试使用 eval() 访问响应文本值。
/*** AJAX ***/
var qty = eval(response.item_quantity);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.