繁体   English   中英

从 jquery ajax 调用返回多个值到 php

[英]Return multiple values from jquery ajax call to php

我正在尝试从 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(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);

$("#shopping-cart-quantity").html(response);

我得到以下...

{ '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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM