Click Here For MCQ

Thursday, June 4, 2020

How to consume api in html or cshtml

when json data is this type 

[{"sno":1,"name":"ritesh","ftnsme":"pandey"},{"sno":2,"name":"rohit","ftnsme":"satya"},{"sno":1002,"name":"babu","ftnsme":"ravi"},{"sno":1003,"name":"rauio","ftnsme":"hugff"},{"sno":2002,"name":"ritesh","ftnsme":"ravi"},{"sno":3002,"name":"mkjuii","ftnsme":"nhuytm"},{"sno":3003,"name":"bhutt","ftnsme":"lpoiu"},{"sno":3004,"name":"bhuttkijj","ftnsme":"lpoiubhutt"},{"sno":3005,"name":"pouytf","ftnsme":"njuug"},{"sno":3006,"name":"babukijj","ftnsme":"njutred"},{"sno":4002,"name":"bhyur","ftnsme":"bhjs"},{"sno":5002,"name":"fatuio","ftnsme":"huopi"},{"sno":6002,"name":"sach","ftnsme":"bach"},{"sno":6003,"name":"gytr","ftnsme":"lopu"},{"sno":6004,"name":"ramesh","ftnsme":"sandeep"},{"sno":6005,"name":"kamesh","ftnsme":"sandeep"},{"sno":6006,"name":"kamesh","ftnsme":"rahul"}]




how to consume api in html or cshtml 





<h2>myjavaapiview</h2>
<div id="listofdata"></div>

<table>
    <tr>
        <td id="list1" width="150"></td>
        <td id="list2" width="150"></td>
        <td id="list3" width="150"></td>
    </tr>
    <br />
</table>
  


<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script>
    var html="";

    $.ajax({
        url: 'http://localhost:2034/api/values',
        type: 'GET',
        dataType: 'json',
        //data: person,
        success: function (data, textStatus, xhr) {
            console.log(data);


            $.each(data, function (key, item ) {

         
                $('#list1').append("<div>" +   item.sno   + "</div>");
                $('#list2').append("<div>" +   item.name  + "</div>");
                $('#list3').append("<div>" +   item.ftnsme   + "</div>");
           
            });

        },

        error: function (xhr, textStatus, errorThrown) {
            console.log('Error in Operation');
        }
    }).done(function ()
    {

    }
    );
</script>




No comments:

Post a Comment