如果你所得到的響應,"regular_price": "800", "discount_price": 720
然後進行它有效的JSON,解析它並獲得屬性。
var obj = JSON.parse('{' + msg + '}');
// valid json -^-----------^-
// get object properties
var regular = data.regular_price;
var discount = data.discount_price;
UPDATE:如果響應數據是有效的JSON格式然後設置dataType: 'json'
選項。
$.ajax({
url: url,
type: 'post',
data: 'id='+id,
// set response datatype as json
dataType:'json',
success: function(msg) {
// get properties
var regular = msg.regular_price;
var discount = msg.discount_price;
}
});
或者直接解析,如果響應是一個字符串。
$.ajax({
url: url,
type: 'post',
data: 'id='+id,
success: function(msg) {
// parse the string
var data = JSON.parse(msg);
// get properties
var regular = data.regular_price;
var discount = data.discount_price;
}
});
什麼是味精? console.log(msg),然後檢查控制檯。 –
'msg.regular_price'和'msg.discount_price'不適合你嗎? –
是的,如果味精是一個對象,我同意@Swaraj Giri –