Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following C# class:

public class JsonBackup {     public int Added { set; get; }     public int DEVCount { set; get; }     public int DS1Count { set; get; }     public IList<ViewEvent> Events { get; set; }     public IEnumerable<string> Errors { set; get; }     public int Rejected { set; get; }     public bool Success { set; get; }     public int Updated { set; get; } } 

and this code to return JSON data to my browser:

return Json(new JsonBackup {     Added = added,     DEVCount = devCount,     DS1Count = ds1Count,     Events = t.Events,     Rejected = rejected,     Success = true,     Updated = updated }); 

The data is returned here:

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

and used in other places and also here:

   $.each(data.Events, function (i, item) {         $("#stats-list li:eq("+(4+i)+")").after('<li>' + item.Description + ' : ' + item.Elapsed + ' ms</li>');     }); 

Is it possible for me to create a TypeScript type and assign data to that type so I could for example get intellisense when selecting such things as

data.Added or data.DEVCount etc? 
like image 556
Samantha J T Star Avatar asked Nov 10 '12 08:11

Samantha J T Star


People also ask

Can we use ajax in TypeScript?

Using the codeMySite - this is where you put page specific or module specifc scripts which uses Ajax module for making ajax calls. In the example above I have provided four different ways you make ajax calls to get customer records and that gave you an idea of how to use other methods.

Is there a JSON type in TypeScript?

Introduction to TypeScript JSON type. The TypeScript comes up with the functionality of working with JSON Type data. JSON being the JavaScript Object Notation, is used to make a data model that is easy to write and read. We can easily analyze large and complex data set with this TypeScript JSON type.

Does ajax allow the use of JSON data?

AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.


1 Answers

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(<IJsonBackup>data, ajaxElapsed);     }); 
like image 89
Slawek Avatar answered Oct 11 '22 18:10

Slawek