Can I create a TypeScript type and use that when AJAX returns JSON data?

前端 未结 1 813
清酒与你
清酒与你 2020-12-24 06:35

I have the following C# class:

public class JsonBackup
{
    public int Added { set; get; }
    public int DEVCount { set; get; }
    public int DS1Count { s         


        
1条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 07:33

    Simplest way to achieve that is to create interface for IJsonBackup and when you receive json just cast it to IJsonBackup

    interface IViewEvent
    {
    }
    
    interface IJsonBackup
    {
        Added : number;
        DEVCount : number;
        DS1Count : number;
        Events : IViewEvent[];
        Errors : string[];
        Rejected : number;
        Success : bool;
        Updated : number;
    }
    

    In your class definition:

    backupDone(data: IJsonBackup, ajaxElapsed: any)
    {
    }
    
    $.ajax("/Backup/Data/Backup",
        {
            cache: false,
            dataType: 'json',
            type: 'POST'
        })
        .done(function (data: any) {
            console.log(data);
            backupDone(data, ajaxElapsed);
        });
    

    0 讨论(0)
提交回复
热议问题