Javascript 从jQueryAjax调用返回多个值到php
我试图从php进程返回多个值 下面是jQuery函数Javascript 从jQueryAjax调用返回多个值到php,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图从php进程返回多个值 下面是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(respon
$.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);
}
});
除了我无法从返回的JSON中检索特定字段值外,上述方法似乎有效
当我尝试这个
var qty = response.item_quantity;
$("#shopping-cart-quantity").html(qty);
什么也没发生
如果我改变
$("#shopping-cart-quantity").html(qty);
到
我得到以下信息
{ 'account_id': '1', 'item_id' : 'cce3d2a017f6f1870ce8480a32506bed', 'item_name' : 'CE', 'item_quantity' : '1', 'item_price' : '1' }
请确保使用json_encode()返回结果数组
/*** PHP ***/
echo json_encode($resultArr); exit ;
在AJAX中,尝试使用eval()访问响应文本值
/*** AJAX ***/
var qty = eval(response.item_quantity);
response
是否有一个responseJSON
属性,该属性包含您要查找的对象?@ScottKaye因为他使用的是数据类型:“json”
,response
已经被解析,它不是XHR。您的json不正确。字符串必须包含在双引号中,而不是单引号中。PHP脚本中有一个bug,它没有调用json\u encode()
对响应进行编码。
/*** AJAX ***/
var qty = eval(response.item_quantity);