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?
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.
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.
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.
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With