如果我有ajax生成的数据对象,我如何访问TotalCashBack值? 我试过var $ total_amt = data.TotalCashBack;但它返回undefined
Object]0: Object
TotalCashBack: "9.00"
__proto__: Object..... function __proto__() { [native code] }length: 1__proto__: Array[0]
这是我的ajax代码:
function getCashBackTotalAmount(u_email){
console.log('cashback value');
$.ajax({
url: 'userprofile/gettotalcashback',
data : {email:u_email},
dataType: 'json',
success: function(data){
console.log('success getTotalCashBack');
console.log(data);
var $total_amt = data.TotalCashBack;
console.log($total_amt);
$('.earning > span').replaceWith($total_amt);
console.log(data.length);
},error : function(){
console.log("error getTotalCashBack");
}
});
}
我实际上使用的是MVC。 这是我的建模者:
function gettotalcashback($email, $mediaid){
$db_user = $this->load->database('cashback_user',TRUE);
$q = "SELECT SUM(cb_amnt) as TotalCashBack from cashback_sale WHERE email='".$email."' AND media_id=".$mediaid;
$q_usr = $db_user->query($q);
if ($q_usr->num_rows() > 0)
{
return ($q_usr->result());
}
}
这是我的控制器:
public function gettotalcashback(){
global $data, $f;
$media = get_cookie('cb_media');
$f = false;
$total_cashback = $this->pfm->gettotalcashback($_GET['email'], $media);
echo json_encode($total_cashback);
}