0

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

SyntaxErrorL JSON.parseLunexpected character 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]
     [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);
            }
        });
4

1 回答 1

0

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

试试这个

{"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"}]}
于 2013-04-12T11:09:59.130 回答