【问题标题】:JsonResult not returning data after Ajax call in .NET Core Razor Page.NET Core Razor 页面中的 Ajax 调用后,JsonResult 未返回数据
【发布时间】: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


    【解决方案1】:

    修复类,移除所有属性并添加getter/setter

    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));
            }, 
            .....
    

    【讨论】:

    • 感谢您的回复。我已将Ajax 呼叫更改为您的答案,现在警报已更改,但仍不包含数据?请查看我更新的问题。
    • @tcode 我更新了答案,再试一次
    • 太棒了——它的工作原理!谢谢!
    • @tcode 欢迎您!
    • @MikeBrind 它写在我的答案中 - 用于测试,在警报窗口中显示数据。如果不进行字符串化,您将无法在警报中显示 json 对象。
    【解决方案2】:

    您可以将返回的对象视为服务器端类的实例:

    success: function (dataPoint) {
        alert(`X: ${dataPoint.x}\nY: ${dataPoint.y}`);
    }
    

    【讨论】:

    • 谢谢你。
    猜你喜欢
    • 2020-02-09
    • 2019-10-19
    • 1970-01-01
    • 2021-01-30
    • 2018-03-06
    • 2020-05-04
    • 2020-11-26
    • 2021-12-05
    • 2018-09-04
    相关资源
    最近更新 更多