我正在尝试从 PHP 脚本返回 json 编码的字符串。
$.post("order.php", { product: product_id, coupon: coupon },
function(data) {
$("#price").html("$" + data.price);
$("#discount").html("$" + data.discount);
$("#total").html("$" + data.total);
});
我尝试在回调中使用警报函数来查看 PHP 的返回值:
{"price":249,"discount":"0.00","total":249}
但是#price 和rest 元素的值为“$undefined”。
请帮忙。
最佳答案
看来您只需使用 parseJSON()
将 JSON 数据解析为对象即可:
$.post("order.php", { product: product_id, coupon: coupon },
function(data) {
data = $.parseJSON( data );
$("#price").html("$" + data.price);
$("#discount").html("$" + data.discount);
$("#total").html("$" + data.total);
});
关于jquery - 使用 jQuery POST 函数从服务器返回 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12816565/