jquery中的json问题


json problem in jquery

我使用以下代码:

$(document).ready(function() {
        $.ajax({
            url: "tiles/chartValue.php",
            datatype: 'json',
            type: 'POST',
            success: function(data){                
                $.each(data, function(key, value) { 
                  alert(key + ': ' + value); 
                });
                       });
                });

这是图表值.php

<?php
    include("../functions.php");
    $DAO=new DBUtil();
    $DAO->initDB("../db/connection.properties");
    $k=$DAO->getChart();
    echo json_encode($k);
?>

下面是函数的 getChart 函数.php

public function getChart()
{
    $sql = "SELECT a.total, round( (b.active *100) / total ) active_user, round( (c.inactive *100) / total ) inactive_user
        FROM 
        (               
            SELECT IFNULL( count( * ) , 0 ) total FROM ashekan_info
        )a, 
        (               
            SELECT IFNULL( count( * ) , 0 ) active FROM ashekan_info WHERE is_active =1
        )b, 
        (               
            SELECT IFNULL( count( * ) , 0 ) inactive FROM ashekan_info  WHERE is_active =0
        )c";
        $result=mysql_query($sql,$this->connect()) or die(mysql_error());   
        $row = mysql_fetch_assoc($result);  
        $json = array('total'       =>$row['total'],
                  'active_user' =>$row['active_user'],
                  'inactive_user'   =>$row['inactive_user']
                 ); 
        return $json;
}

我得到火虫的结果是{"总计":"1","active_user":"0","inactive_user":"100"}

问题是它在警报框中显示每个字符,即:{,",T等。

我怎样才能得到 1, 0, 100 ? 提前感谢。

只是一个猜测...

您有 datatype: 'json' ,但 JS 区分大小写,正确的参数是 dataType 。因此,它忽略了这一点,获取文本,您的each调用正在遍历字符串的字符。

因为您从 AJAX 调用中获得的答案仍然是一个字符串。首先使用 JQuery.ParseJson 解析它,然后迭代解析的变量。

我认为你需要的是:

header("Content-type: application/json");

就在之前

echo json_encode($k);