4

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.

9
  • what problem you are facing? Commented Dec 14, 2015 at 6:17
  • I don't see an Element with class .content declared in your HTML. Commented Dec 14, 2015 at 6:18
  • You must have .content div present to append ul & li. Commented Dec 14, 2015 at 6:28
  • @ParthTrivedi I am not able to see anything on my screen after pressing the button MartinSchneider: I have one Div tag with the class content in my code. Commented Dec 14, 2015 at 6:29
  • you should have div .content in your HTML already in DOM. Commented Dec 14, 2015 at 6:32

4 Answers 4

2
$.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>");

});
Sign up to request clarification or add additional context in comments.

Comments

1

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>");
});

Comments

0

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>');
            }
        });
 });

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.