为什么Google Chart不能运行ASP MVC应用程序?

时间:2016-06-18 16:47:11

标签: javascript json asp.net-mvc

我的控制器上有方法,返回Json

        [HttpPost]
        public JsonResult CompanyChart()
        {

            var data = db.adusers;
            var selectUsers = from s in data where (s.Company != null) select s;
            int fenixPlus = (from fP in data where (fP.Company == "ООО \"Феникс+\"") select fP).Count();
            int fenixPresent = (from fP in data where (fP.Company == "ООО \"Феникс-Презент\"") select fP).Count();

            var dataObj = new object[]
            {
                new object[] {"Компания","Количество"},
                new object[] { "ООО \"Феникс+\"", fenixPlus},
                new object[] { "ООО \"Феникс-Презент\"", fenixPresent}
            };

            return Json(dataObj, JsonRequestBehavior.AllowGet);
        }
杰森看起来像这样  [["Компания","Количество"],["ООО \"Феникс+\"",53],["ООО \"Феникс-Презент\"",42]]

在我看来,我有一个jquery帖子请求

  $.ajax({
                url: '@Url.Action("CompanyChart", "Users")',
                type: 'post',
                dataType: "json",
                success: function (data) {
                    drawChart(data);
                }
            });   

然后,我尝试绘制谷歌饼图

google.charts.load("current", { packages: ["corechart"] });
            google.charts.setOnLoadCallback(drawChart);
            function drawChart(data) {

                var array = JSON.parse(data);
                var dataV = new google.visualization.DataTable(array);
                var options = {
                    title: 'My Daily Activities',
                    pieHole: 0.4,
                };

                var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
                chart.draw(dataV, options);
            };

无效,控制台Uncaught SyntaxError: Unexpected token К in JSON at position 0中的错误

我不明白,为什么我的代码不起作用?

1 个答案:

答案 0 :(得分:1)

JsonResult向调用方返回JSON object而不是JSON内容字符串,因此,您无需调用JSON.parse

只需更改

var array = JSON.parse(data);

var array = data;