0

I'm attempting to parse some JSON results from a MySQL database into a simple HTML table. Currently, I can return the raw JSON string data correctly. However, when I try to parse these results into an HTML table, I am returned 'Undefined'.

JS File:

$('input#name-submit').on('click', function() {
var name = $('input#name').val();
if ($.trim(name) != '') {
$.post('ajax/name.php', {name: name}, function(data) {
 var tr;
    for (var i = 0; i < data.length; i++) {
        tr = $('<tr/>');
        tr.append("<td>" + data[i].ARTIST + "</td>");
        tr.append("<td>" + data[i].LOCATION + "</td>");
        $('table').append(tr);
        }
});
}

});

table from HTML file:

<table>
<tr>
    <th>ARTIST</th>
    <th>LOCATION</th>
</tr>
</table>

Edited:

The JSON results, being returned as 'Undefined' in my index file

{"ARTIST":"Katy Perry","LOCATION":"United States"}

2
  • About the table HTML... the first part should be enclosed in a <thead> element rather than <tr>. Then, <tbody> for the content. Commented Dec 22, 2013 at 3:40
  • Offtopic, but this might be interesting for you: handlebarsjs.com Commented Dec 22, 2013 at 14:55

2 Answers 2

4

jQuery is intelligent about determining the return type of ajax requests based on a lot of factors. In this case, I'm almost positive that data has already been parsed. If you remove the sentdata line and just use data instead, your code should work as you intend.

See the dataType property for ajax for jQuery

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

7 Comments

Ah yes I thought this line could be surplus to requirements. I've removed it, however, i'm now only being returned an 'undefined' result from the JSON in each column.
@Byate have you inspected the return from your Ajax request in firebug/Web Inspector/F12 tools?
I've inspected in Firebug and the JSON is loading as inspected. It's parsing it into the HTML table where the problem begins, resulting in 'Undefined' results
@Byate when does it return "'Undefined' results?"
This is displayed when I view my index file in my browser. When debugging the page, the page is successfully returning the JSON data but displaying it as 50 'Undefined' records where the parsed JSON data should be. Stumped!
|
1

An exammple is here: http://jsfiddle.net/nqRk9/2/

if your data is:

{"ARTIST":"Katy Perry","LOCATION":"United States"}

You could just do this without the for loop:

    tr = $('<tr/>');
    tr.append("<td>" + data.ARTIST + "</td>");
    tr.append("<td>" + data.LOCATION + "</td>");
    $('#single').append(tr);

But if you were to have more artists and locations here is an example of what your data could look like.

{"ARTIST":[["Katy Perry"], ["Artist 2"], ["Artist 3"]],"LOCATION":[["United States"],["Location 2"], ["Location 3"]]};

And here's how you could access the data:

for(var i = 0; i < data.ARTIST.length; ++i)
{
        tr = $('<tr/>');
        tr.append("<td>" + data.ARTIST[i] + "</td>");
        tr.append("<td>" + data.LOCATION[i] + "</td>");
        $('#multiple').append(tr);
}

I believe that your array subscript is in the wrong place, In the OP.

1 Comment

Thanks - it didn't solve the particular problem I had but this is a nice way of doing it and i've adapted it into my code. I added a variable var sentdata=JSON.parse(data) just before the for loop and and changed data to sentdata. This now parses the data as desired.

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.