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

时间:2022-09-25 16:00:25

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' }

1 个解决方案

#1


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

#1


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