在 .NET Core Razor 页面中调用 Ajax 后 JsonResult 未返回数据
JsonResult not returning data after Ajax call in .NET Core Razor Page
我正在尝试从 ASP.Net Core (3.1) Razor Page Application 中的 JsonResult
方法返回数据一个 Razor 页面,但是,我遇到了问题。
当我调试时,我可以看到页面模型中的 OnGetCarList
方法被命中,并且当我单步执行时代码中没有错误,但是,当数据被 returned 到Ajax Success
函数和带有 alert
的输出,这是我看到的:
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)
}
})
修复 class ,删除所有属性并添加 getters/setters
public class DataPoint
{
public DataPoint(double x, double y)
{
X = x;
Y = y;
}
public double? X {get; set;}
public double? Y {get; set;}
}
使用JSON.stringify进行测试
$.ajax({
....
success: function (data) {
alert(JSON.stringify( data));
},
.....
您可以将返回的对象视为服务器端的一个实例class:
success: function (dataPoint) {
alert(`X: ${dataPoint.x}\nY: ${dataPoint.y}`);
}
我正在尝试从 ASP.Net Core (3.1) Razor Page Application 中的 JsonResult
方法返回数据一个 Razor 页面,但是,我遇到了问题。
当我调试时,我可以看到页面模型中的 OnGetCarList
方法被命中,并且当我单步执行时代码中没有错误,但是,当数据被 returned 到Ajax Success
函数和带有 alert
的输出,这是我看到的:
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)
}
})
修复 class ,删除所有属性并添加 getters/setters
public class DataPoint
{
public DataPoint(double x, double y)
{
X = x;
Y = y;
}
public double? X {get; set;}
public double? Y {get; set;}
}
使用JSON.stringify进行测试
$.ajax({
....
success: function (data) {
alert(JSON.stringify( data));
},
.....
您可以将返回的对象视为服务器端的一个实例class:
success: function (dataPoint) {
alert(`X: ${dataPoint.x}\nY: ${dataPoint.y}`);
}