开发者

json problem in jquery

I am using the following code :

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

Here is chartValue.php

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

here is the getChart function of functions.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开发者_JAVA百科_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;

}

I get the result in firebug is {"total":"1","active_user":"0","inactive_user":"100"}

problem is it shows each character in alert box. i.e : {, ", t and so on.

how can i get 1, 0, 100 ? thanks in advance.


Just a guess...

You have datatype: 'json', but JS is case sensitive, and the correct parameter is dataType. So it's ignoring that, taking text, and your each call is iterating through characters of a string.


Because the answer you get from your AJAX call is still a string. Parse it first with JQuery.ParseJson, then iterate the parsed variable.


what I think you need is :

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

right before

echo json_encode($k);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜