-2

I have a simple JavaScript code which using Ajax. I want translate this ajax query to TypeScript for more object oriented view. With class and method and hard data types (similar to Java:-).

Help me translate if it posible:

$(document).ready(function() {
    $('#my-viewscope').click(function() {
        $ajax({
            url: 'get_data_servlet',
            type: 'post',
            dataType: 'json',

            success: function(response) {
                $.each(response, function(key, value) {
                    console.log(value);
                });
            }
        })
    })
});
Pavel
  • 2,005
  • 5
  • 36
  • 68
  • 2
    For simple uses like this it's better this way. – error404 Jul 20 '17 at 15:41
  • Why don't you just setup an interface that type checks the response? and handle it that way. – Win Jul 20 '17 at 15:44
  • 2
    Related : [How to use jQuery with TypeScript](https://stackoverflow.com/questions/32050645/how-to-use-jquery-with-typescript) – Pac0 Jul 20 '17 at 15:46
  • @Anamul Hasan for simple yes. But this example for understand principle my real code different. – Pavel Jul 20 '17 at 15:47

1 Answers1

0

A simple, but slightly dated article explains the basics of how to wrap your API calls in a TypeScript class. Pay attention to the Customer class, which contains four functions - SelectAll(), Insert(), Update() and Delete() that call the respective Web API methods using jQuery, all while maintaining type-checking (alas, I was once a Java developer who ran into the same concerns when learning dynamically typed Javascript):

raychz
  • 1,085
  • 1
  • 8
  • 24