Akki
Akki

Reputation: 2359

Displaying the JSON data in HTML+jQuery

I have a REST API which produces JSON Output as follows:

    [{"case": 2005608875, 
  "filepath": "/x/eng/cs-data/latx/dev/20150510_uploads/wilp/perfstat_20150415_001256/node/10.95.172.19/output.data", 
  "datatype": "perf8",
  "perfdateend": "2015-04-15T02:15:37-04:00", 
  "userid": "wilp", 
  "filename":"perfstat_20150415_001256.zip",
  "version": "v8.1 ", 
  "hosts": [{"filer": "cluster1-01", 
  "hostname": "10.95.172.18", }],
  "perfid":"98"}]

I am trying to display this data in HTML , but I am not able to do so, Here is my div of HTML+jQuery:

<div class="widgetcontent">

                                <select>
                                <option>
                                96  
                                </option>
                                <option>
                                97
                                </option>
                                <option>
                                98
                                </option>
                                <option>
                                99
                                </option>
                                </select>

                                   <button class="topcoat-button--cta">Get Data!</button>



                    </div><!--widgetcontent-->
<div class='content'> </div>
                    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
                    <script src="js/pull.js"></script>

and here is my jQuery(pull.js)

(function ($) {
$('button').on('click', function () {
    // remove resultset if this has already been run
    $('.content ul').remove();


    // get selected zip code from selectbox
    var perfid = $('select option:selected').text();

    // make AJAX call
    $.getJSON('http://myapiurl.com/ws/spm/search/perfid/' +perfid, function (data) {

        // do all this on success       
        var items = [],
            $ul;

        $.each(data, function (key, val) {
            //iterate through the returned data and build a list
            items.push('<li id="' + key + '"><span class="Case">' + val.case + '</span><br><span class="UserID">' + val.userid + '</span></li>');
        });
        // if no items were returned then add a message to that effect
        if (items.length < 1) {
            items.push('<li>No Data Found!</li>');
        }




        // append list to page
        $ul = $('<ul />').appendTo('.content');

        //append list items to list
        $ul.append(items);
    });
});
  }(jQuery));

I am pulling my hair on this since 2 days and still cannot figure it out.

Upvotes: 4

Views: 163

Answers (4)

waqas jamil
waqas jamil

Reputation: 423

$.getJSON('http://myapiurl.com/ws/spm/search/perfid/' + perfid, function (data) {

    // do all this on success  
    var itemsHtml = '<li>No Data Found!</li>';
    if (data.length) {  

        itemsHtml = "",        

        $.each(data, function (key, val) {
            //iterate through the returned data and build a list
            itemsHtml+= '<li id="' + key + '"><span class="Case">' + val.case + '</span><br><span class="UserID">' + val.userid + '</span></li>';
        });

    }

    $(".content").html("<ul>"+itemsHtml+"</ul>");

});

Upvotes: 2

The only thing I see wrong is that you need a div with class="content":

<div class='content'></div>

That's a bit dangerous though, because a lot of things use "content" as a class.

Other than that, sometimes the json results are nested inside "results", so you've got to access the data through "results.data" instead of just "data" Anyway, here's a jsfiddle without the restful call:

https://jsfiddle.net/mckinleymedia/n48s458v/1/

Upvotes: 0

madalinivascu
madalinivascu

Reputation: 32354

try:

$('button').on('click', function () {
    // remove resultset if this has already been run
    $('.content ul').empty();


    // get selected zip code from selectbox
    var perfid = $('select option:selected').text();

    $.getJSON('http://myapiurl.com/ws/spm/search/perfid/' +perfid, function (data) {


            $.each(data, function (key, val) {
                //iterate through the returned data and build a list
                $('.content ul').append('<li id="' + key + '"><span class="Case">' + val.case + '</span><br><span class="UserID">' + val.userid + '</span></li>');
            });
            // if no items were returned then add a message to that effect
            if (data.length < 1) {
                $('.content ul').append('<li>No Data Found!</li>');
            }
        });
 });

Upvotes: 0

Parth Trivedi
Parth Trivedi

Reputation: 3832

your jQuery should be

$.getJSON('http://myapiurl.com/ws/spm/search/perfid/' + perfid, function (data) {

    // do all this on success       
    var itemsHtml = "",        

    $.each(data, function (key, val) {
        //iterate through the returned data and build a list
        itemsHtml+= '<li id="' + key + '"><span class="Case">' + val.case + '</span><br><span class="UserID">' + val.userid + '</span></li>';
    });
    // if no items were returned then add a message to that effect
    if (data.length == 0) {
        itemsHtml = '<li>No Data Found!</li>';
    }

    // append list to page
    $(".content").append("<ul>"+itemsHtml+"</ul>");
});

Upvotes: 1

Related Questions