Home > Blockchain >  JsonResult not returning data after Ajax call in .NET Core Razor Page
JsonResult not returning data after Ajax call in .NET Core Razor Page

Time:11-20

I'm trying to return data from a JsonResult method within my ASP.Net Core (3.1) Razor Page Application back to a Razor Page, however, I'm having problems.

When I debug, I can see the OnGetCarList method within the Page Model being hit and no errors in the code when I step through, however, when the data is returned to the Ajax Success function and output with an alert, this is what I see:

enter image description here

Ajax Call (within Razor Page)

$.ajax({
          method: 'get',
          url: '/SPC/Index?handler=CarList',
          contentType: "application/json",
          dataType: "json",
          success: function (data) {
                alert(data);
                //addData(data)
                }
       })

Page Model

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;
}

Any guidance is appreciated.

Thanks.

Update

I updated my Ajax call to what Serge said and now the Alert gives this.

$.ajax({
                method: 'get',
                url: '/SPC/Index?handler=CarList',
                contentType: "application/json",
                dataType: "json",
                success: function (data) {
                    alert(JSON.stringify(data));
                    //addData(data)
                }
       })

enter image description here

CodePudding user response:

fix class , remove all attributes and add 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;}

}

use JSON.stringify for test

        $.ajax({
        ....
        success: function (data) {
                alert(JSON.stringify( data));
        }, 
        .....
  • Related