我有一个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);
}
});
答案 0 :(得分:0)
您返回的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"}]}