Jquery返回“Undefined”

时间:2015-11-30 22:35:34

标签: jquery undefined

我们正在绘制来自Jquery的Google图表数据。无论怎样,“data.d”都是未定义的。它没有抛出异常,只是谷歌抱怨我们传递了错误。

退后一步,看看论证的内容......好吧,这实际上是未定义的。

ManageProfile.aspx

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="head">
    <style>
        #chart_div {
            width: 100%;
            height: 100%;
        }
    </style>  
    <!--Load the AJAX API-->
    <script src="assets/js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>

    <script>
        var chartData; // globar variable for hold chart data
        google.load("visualization", "1", { packages: ["corechart"] });

        // Here We will fill chartData

        $(document).ready(function () {

            $.ajax({
                url: "ManageProfile.aspx/GetChartData",
                data: "",
                dataType: "json",
                type: "POST",
                contentType: "application/json; chartset=utf-8",
                success: function (data) {
                    chartData = data.d;
                },
                error: function () {
                    alert("Error loading chart data! Please try again.");
                }
            }).done(function () {
                // after complete loading data
                google.setOnLoadCallback(drawChart);
                drawChart();
            });
        });


        function drawChart() {
            var data = google.visualization.arrayToDataTable(chartData);
            //alert("success");
            var options = {
                title: "Company Performance Product Category Wise",
                pointSize: 5
            };

            var barChart = new google.visualization.BarChart(document.getElementById('chart_div'));
            barChart.draw(data, options);

            window.addEventListener('resize', function () {
                barChart.draw(data, options);
            }, false);
        }  

    </script>

</asp:Content>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<div id="chart_div" style="height:500px"></div>
</asp:Content>

ManageProfile.aspx.cs

using System.Web.Services;
using System.Web.Script.Services;

namespace ASPGoogleChart.Dcr_Logged
{
    public partial class ManageProfile : System.Web.UI.Page
    {
        protected void Page_Load()
        {
            GetChartData();

        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static object[] GetChartData()
        {
            var chartData = new object[2];
            chartData[0] = new object[]{
                "Year",
                "Electronics"
            };

            chartData[1] = new object[]{
                20,
                40
            };
            return chartData;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

 google.setOnLoadCallback(drawChart);
 drawChart();

将这些转移到ajax成功未完成。