即使我从服务器传递到客户端的数据与 Flot 接受的数据相同,但它没有被绘制。在我试图解析返回的字符串 JSON 的行上也有一个错误。下面的代码:
//server_processing.aspx.cs
[WebMethod]
public static string GetPieData()
{
List<PieData> pieData = new List<PieData>();
pieData.Add(new PieData { label = "A", data = 40 });
pieData.Add(new PieData { label = "B", data = 40 });
pieData.Add(new PieData { label = "C", data = 20 });
var serializer = new JsonSerializer();
var stringWriter = new StringWriter();
var writer = new JsonTextWriter(stringWriter);
writer.QuoteName = false;
serializer.Serialize(writer, pieData);
writer.Close();
var json = stringWriter.ToString();
return json;
}
//PieData.cs
[JsonObject(MemberSerialization.OptIn)]
public class PieData
{
[JsonProperty]
public string label { get; set; }
[JsonProperty]
public int data { get; set; }
}
<%-- Charts.aspx --%>
<script type="text/javascript">
var options = { series: { pie: {show: true} } };
$(document).ready(function () {
$.ajax({
type: "POST",
url: "server_processing.aspx/GetPieData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
var jsObj = [];
//Error on the line below
//0x800a03f6 - JavaScript runtime error: Invalid character
jsObj.push($.parseJSON(msg.d)); // [{label:"A",data:40},{label:"B",data:40},{label:"C",data:20}]
$.plot($("#piechart"), jsObj, options);
}
});
});
我对 Flot 折线图做了同样的事情,而且效果很好。有人可以指出我哪里出错了吗?