dddexxx
dddexxx

Reputation: 31

Error while looping in ajax call

I am trying to print the results of an specific json object. I am receiving

Uncaught TypeError: undefined is not a function

while trying to execute the following code:

$.getJSON("http://test.test.com/api/service/Something()?username=" + username + "&password=" + password + "&$format=json", function( d ){
    var tr;
    results = d.d.results;
    for (var i=0; i<results.length; i++){
        tr = ("</tr>");
        tr.append("<td>" + results[i].something + "</td>");
        tr.append("<td>" + results[i].something_2 + "</td>");
        tr.append("<td>" + results[i].something_3 + "</td>");
        $('table').append(tr);
    }

I pretty much copied the code above from another question, obviously I am doing something wrong. The error comes here > tr.append("" + results[i].something + "")

Upvotes: 0

Views: 78

Answers (3)

Scimonster
Scimonster

Reputation: 33409

tr = $("<tr/>");

You were missing the $, and had the / in the wrong place.

Also:

tr.append("<td>" + results[i].something + "</td>");
          ^^^^^ missing

Upvotes: 2

David
David

Reputation: 218960

tr doesn't have a function called append, because you never defined one. Here's how you're creating tr:

var tr;
tr = ("</tr>");

So tr is likely just a string at this point. You never define any functions on it, so this won't work:

tr.append(...);

Perhaps you meant to create a jQuery object?:

tr = $("</tr>");

Though in that case you're likely also looking to create the element, not close it:

tr = $("<tr/>");

(Note also that your first append() is missing the opening td element.)

Upvotes: 1

Think Different
Think Different

Reputation: 2815

Change

tr = ("</tr>");
tr.append("" + results[i].something + "</td>");

TO

tr = $("<tr/>");
tr.append("<td>" + results[i].something + "</td>");

Upvotes: 0

Related Questions