我的ajax函数:
function fetchData(varUrl, varFunc, varData) {
if (varData == undefined) varData = "";
var options = {
type: "POST",
url: varUrl,
data: varData,
contentType: "application/json;charset=utf-8",
dataType: "json",
timeout: 8000,
cache: false,
beforeSend: ShowLoading(),
success: varFunc,
complete: HideLoading(),
error: function (x, t, m) {
if (t === "timeout") {
alert("got timeout");
} else {
alert(t+"\r\n\r\n"+m);
}
}
};
//execute the ajax call and get a response
var request = $.ajax(options);
}
function funcA(data){
$.each(data, function (i, item) {...}
}
function funcB(data){
$.each(data, function (i, item) {...}
}
C#代码:
public ActionResult AjaxA()
{
try
{
using (var db = new MvcContext())
{
var history = from h in db.Histories.Where(h => h.Id == 2)
return this.Json(history.ToList());
}
}
catch(Exception)
{
return this.Json("");
}
}
public ActionResult AjaxB()
{
var history = new List<MyDataType>();
history.Add( new MyData(...));
...
history.Add( new MyData(...));
return this.Json(history);
}
1)如果我使用 fetchData("AjaxA", funcA) 来获取数据,就可以了
2)如果我使用 fetchData("AjaxB", funcB) 来获取数据,它会失败并得到错误
JSON.parse:意外字符错误
但在这种情况下,firebug 也可以获得良好的 json 数据。
json 数据 [{"x":39,"y":115.5,"Data":16.743013706957576},{"x":39.5,"y":115.5,"Data":12.353109643128887}]