我的请求中有这个json
$.get("/shop_possystems/index.php?route=module/cart/ajax_get_individual_prices¤t_id=" + current_id + "&standard_id=" + standard + "&professional_id=" + professional + "&premium_id=" + premium + "&quantity=" + quantity,
function(data) {
var standard_price = data.standard_price;
var professional_price = data.professional_price;
var premium_price = data.premium_price;
console.log(data);
$prettyCheckBox0.text(standard_price);
$prettyCheckBox1.text(professional_price);
$prettyCheckBox2.text(premium_price);
});
问题是data.standard_price返回undefined但在console.log中我有这个
"{"standard_price":"included","professional_price":"add $792.00","premium_price":"add $3372.00"}"
这笔交易是什么
答案 0 :(得分:5)
您需要解析JSON。把它作为回调的第一行:
data = $.parseJSON(data);
或者,使用$.getJSON
代替$.get
或将"json"
作为$.get
的另一个参数传递。
答案 1 :(得分:2)
您应该使用$ .parseJSON(string),如下所示:
$.get("/shop_possystems/index.php?route=module/cart/ajax_get_individual_prices¤t_id=" + current_id + "&standard_id=" + standard + "&professional_id=" + professional + "&premium_id=" + premium + "&quantity=" + quantity,
function(data) {data = $.parseJSON(data);
var standard_price = data.standard_price;
var professional_price = data.professional_price;
var premium_price = data.premium_price;
console.log(data);
$prettyCheckBox0.text(standard_price);
$prettyCheckBox1.text(professional_price);
$prettyCheckBox2.text(premium_price);
});
另一种方法是在dataType: 'json'
中使用$ .ajax指定http://api.jquery.com/jQuery.ajax/
答案 2 :(得分:1)
console.log
似乎表明你有一个字符串,而不是一个真正的Javascript对象。您需要告诉jQuery将内容视为JSON并为您提供已解析的对象。最简单的方法是使用$.getJSON
代替$.get
:
$.getJSON("/shop_possystems/index.php?route=module/cart/ajax_get_individual_prices¤t_id=" + current_id + "&standard_id=" + standard + "&professional_id=" + professional + "&premium_id=" + premium + "&quantity=" + quantity,
function(data) {
var standard_price = data.standard_price;
var professional_price = data.professional_price;
var premium_price = data.premium_price;
console.log(data);
$prettyCheckBox0.text(standard_price);
$prettyCheckBox1.text(professional_price);
$prettyCheckBox2.text(premium_price);
});
答案 3 :(得分:1)