【发布时间】:2021-12-30 09:04:16
【问题描述】:
我正在尝试从 ASP.Net Core (3.1) Razor 页面应用程序 中的 JsonResult 方法将数据返回到 Razor 页面,但是我遇到了问题。
当我调试时,我可以看到页面模型中的 OnGetCarList 方法被命中,并且当我单步执行时代码中没有错误,但是,当数据返回到 Ajax Success 函数并使用 @ 输出时987654328@,这是我看到的:
Ajax 调用(在 Razor 页面内)
$.ajax({
method: 'get',
url: '/SPC/Index?handler=CarList',
contentType: "application/json",
dataType: "json",
success: function (data) {
alert(data);
//addData(data)
}
})
页面模型
public JsonResult OnGetCarList()
{
var converted = DateTime.Now.ToOADate();
DateTime one = DateTime.Now;
DateTime two = DateTime.Now.AddDays(1);
DateTime three = DateTime.Now.AddDays(2);
DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var c_one = (long)(one - sTime).TotalMilliseconds;
var c_two = (long)(two - sTime).TotalMilliseconds;
var c_three = (long)(three - sTime).TotalMilliseconds;
dataPoints = new List<DataPoint>();
dataPoints.Add(new DataPoint(c_one, 100));
dataPoints.Add(new DataPoint(c_two, 200));
dataPoints.Add(new DataPoint(c_three, 300));
return new JsonResult(dataPoints);
}
//DataContract for Serializing Data - required to serve in JSON format
[DataContract]
public class DataPoint
{
public DataPoint(double x, double y)
{
this.x = x;
this.Y = y;
}
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "x")]
public Nullable<double> x = null;
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "y")]
public Nullable<double> Y = null;
}
感谢任何指导。
谢谢。
更新
我将我的 Ajax 调用更新为 Serge 所说的内容,现在 Alert 给出了这个。
$.ajax({
method: 'get',
url: '/SPC/Index?handler=CarList',
contentType: "application/json",
dataType: "json",
success: function (data) {
alert(JSON.stringify(data));
//addData(data)
}
})
【问题讨论】:
标签: c# json asp.net-core razor-pages jsonresult