【问题标题】:JQPlot and C# response issuesJQPlot 和 C# 响应问题
【发布时间】:2013-04-12 11:05:01
【问题描述】:

我有一个JQPlot图表,调用一个WebMethod是c#。 WebMethod 调用 sp 并返回日期和金额。我遇到了返回数据的问题。 Firebug 报告以下内容:

SyntaxErrorL JSON.parseLunexpected 字符 var = series = JSON.parse(data.d);

我的 WebMethod 的响应是:

{"d":[{"__type":"GlobalClasses+jqplotModel","amount":25000.00,"ValueDate":"2013-03-27"},    
{"__type":"GlobalClasses+jqplotModel","amount":10000.00,"ValueDate":"2013-03-27"}, 
{"__type":"GlobalClasses+jqplotModel","amount":200000.00,"ValueDate":"2013-03-27"},
{"__type":"GlobalClasses+jqplotModel","amount":69900.00,"ValueDate":"2013-03-27"},]}

WebMethod 代码:

     [WebMethod]
     [ScriptMethod]
     public static List<GlobalClasses.jqplotModel> BuildCharts()
     {


    List<GlobalClasses.jqplotModel> newChart = new List<GlobalClasses.jqplotModel>();

    using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings 
     ["Conn"].ConnectionString))
     {
        using (SqlCommand cmd = new SqlCommand("ChartData_Sel", conn))
        {

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@senderBIC", SqlDbType.VarChar, 11).Value = "ARBUGB2LXXX";
            cmd.Parameters.Add("@messageType", SqlDbType.VarChar, 3).Value = "103";
            cmd.Connection = conn;


            conn.Open();
            using (SqlDataReader dreader = cmd.ExecuteReader())
            {
                while (dreader.Read())
                {
                    GlobalClasses.jqplotModel dataSet = new GlobalClasses.jqplotModel()
                    {
                        amount = dreader.GetDecimal(0),
                        ValueDate = dreader.GetString(1)
                    };
                    newChart.Add(dataSet);
                }
            }

        }

      }
        return newChart;
    }

像这样从我的 HTML 页面调用:

        $.ajax({
            type: "POST",
            url: "Default.aspx/BuildCharts",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var series = JSON.parse(data.d);
                chart = $.jqplot("chart", [series], chartOptions);
            }
        });

【问题讨论】:

    标签: c# jquery jqplot webmethod


    【解决方案1】:

    您返回的 JSON 格式不正确,因为您在最后一项末尾有一个额外的 ,。你可以检查它here。如果你删除它,你应该不会有解析错误。

    试试这个

    {"d":[{"__type":"GlobalClasses+jqplotModel","amount":25000,"ValueDate":"2013-03-27"},{"__type":"GlobalClasses+jqplotModel","amount":10000,"ValueDate":"2013-03-27"},{"__type":"GlobalClasses+jqplotModel","amount":200000,"ValueDate":"2013-03-27"},{"__type":"GlobalClasses+jqplotModel","amount":69900,"ValueDate":"2013-03-27"}]}
    

    【讨论】:

    • 这只是这个页面上的一个类型。响应对象有几千行长,所以我截断了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多