1

I have to display a JSON data in a web page on button click

<table>
  <tr>
    <td style="height:10px;">
      <div id="testResult" style="padding-left: 120px; display: none; ">
        <img src="./images/spinner.gif" />Running the test...
      </div>
    </td>
  </tr>
</table>

It s the html data am using

Bellow is the code written in the button click

    $('#runBtn').click(function() {
         $.get('/getReport', function (data) {
alert(data) // prints [{ "id" : 16, "jobid" : "49", "status" : "Falied" }]
        $('#testResult').html(data);
        });
    }

But nothing is showing in the web page

5
  • Isn't your testResult element hidden? <div id="testResult" style="... display: none; "> Commented Jan 16, 2014 at 10:02
  • I assume that's not all of your HTML, since there's no element with an ID of runBtn in there. Commented Jan 16, 2014 at 10:09
  • Since alert() doesn't print [object Object] this means that jQuery doesn't parse the response, you should either parse the JSON yourself using JSON.parse() or pass the dataType as the third argument to $.get(url, fn, 'json') function. Commented Jan 16, 2014 at 10:11
  • @BlackSheep Or use jQuery.getJSON(). If they parse it from a string to an array, however, they can't just pass that directly to .html() - jQuery has no idea what to do with that (it just seems to fail silently). Commented Jan 16, 2014 at 10:13
  • @AnthonyGrist Yes, .html() doesn't do anything with that array, I assumed setting the JSON response as innerHTML of an element is not the OP's final goal. Commented Jan 16, 2014 at 10:15

3 Answers 3

1

The div with the id "testResult" is hidden. Change your Jquery to the following example, to make it visible.

$('#runBtn').click(function() {
    $.get('/getReport', function (data) {
        alert(data) // prints [{ "id" : 16, "jobid" : "49", "status" : "Falied" }]
        $('#testResult').html(data).show();
    });
}

I made an example on jsfiddle. Try it here: jsfiddle.net/ph3nx/F6uAn/4/

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

1 Comment

Or chain the functions: $('#testResult').html(data).show();
1

Also you need to loop the data for getting each items from json object:

$.each(data, function(index, value) {
  alert(index + ': ' + value.Name);
});

1 Comment

It's never actually parsed so it's just a string, not an array.
0

The 'display: none' part of your html div (with id 'testResult') prevents this div from showing. You should change/remove it.

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.