jquery - 使用 jQuery POST 函数从服务器返回 JSON 数据

标签 jquery ajax json post get

我正在尝试从 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/

相关文章:

jquery - IE7 杀死了我在主导航菜单上的 jQuery 下拉菜单

javascript - 浏览器不发送 CORS HTTP POST 请求选项

javascript - 创建 AJAX 调用时 - 我需要请求什么样的数据?

javascript - 如何使用 jQuery 过滤对象数组

node.js - 如何用 JSON 表示元数据

jquery - 如何在 .show jquery 之后添加时间延迟

javascript - 将整个表格置于其外部 div 的中心

javascript - 如何在 Mithril 中使用 m.request 对模型进行单元测试?

ios - JSON 请求后使用新数据刷新 View Controller

javascript - jQuery 滚动效果无法按预期工作