Ajax返回格式化的整数

时间:2016-08-13 23:18:50

标签: php jquery mysql ajax

我有一个SQL语句来检索以2位小数格式化的int值:

SELECT FORMAT(`budget`.`transportation`,2), FORMAT(`budget`.`accomodation`,2) 
FROM `budget`

我的json_encode返回:

{
 "FORMAT(`budget`.`transportation`,2)":"343.00",
 "FORMAT(`budget`.`accomodation`,2)":"343.00"
}

我希望以$("#transportation")$("#accomodation")

的形式显示它

以下是我处理它的Ajax的一部分:

var budget       = $("#modal-budget"), 
    activityName = $("#modal-activityName"); 

$.ajax({
        type: "POST",
        url: "Ajax.php",
        data: {
                // Data to server
              },
        success : function(data) {
            // Parse result as JSON
            var res = JSON.parse(data);
...
// Update modal fields
transportation . text(res.transportation);
accomodation . text(res.accomodation);

问题在于Ajax返回的值:

{
 "FORMAT(`budget`.`transportation`,2)":"343.00"
 ...
}
// Instead of: 
{
 "transportation":"343.00", 
 "accomodation":"343.00"
}

有没有办法解决这个问题或为每个字段制作Ajax返回货币格式?

1 个答案:

答案 0 :(得分:1)

使用AS设置别名

SELECT FORMAT(budget.transportation,2) AS transportation, 
       FORMAT(budget.accomodation,2) AS accomodation 
FROM budget

然后你的返回json数据应该是这样的:

{"transportation":"343.00", "accomodation":"343.00"}

希望这会对你有所帮助。