Wednesday, April 20, 2016

Mustache JS Ajax call

Earlier we saw how we can use MustachJs to display the hardcoded data in the browser.
Now lets call our MVC controller thru ajax  and get the data and then display it on browser


For this additional we got to add Jquery CDN for ajax calls


<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>


Below is the script to make an ajax "Get" call to the controller and display the data



<script>

        var template,  html;

     

        $.getJSON('http://localhost:52456/api/RiskDetailApi', function (data) {

            debugger

            template = "<h2>RiskDecription: {{RiskDecription}} </h2>";

           

            html = Mustache.render(template, data[0]);

            document.write(html);

        });

     

      

      

    </script>




$.getJSON converts the data to Json format
Run the solution and BOOOM!!! you see the dynamic data on the browser


No comments:

Post a Comment