user2974789
user2974789

Reputation:

Error: Get JSON Data from Web API Using Jquery

I'm really new at this. And I'm really stuck. I have the jquery code, it will load data from Web API, but it does not display on my page.

$.getJSON("/api/Order", function(data) {
            if (data != null) {
                var str = '';
                $.each(data, function (item) {
                    str = '<li>' + item.ItemName + '</li>';
                });
                $("#contents").append(str);
            }
        });

Can anyone explain what is going on? Thanks.

Upvotes: 0

Views: 188

Answers (1)

user1159872
user1159872

Reputation:

try this:

$.getJSON("/api/Order", function(data) {
            if (data != null) {
                var str = '';
                $.each(data, function (key,item) {
                    str = '<li>' + item.ItemName + '</li>';
                });
                $("#contents").append(str);
            }
        });

I added "key" in $.each(data, function (key,item), because data returned from Web API is JSON type.

Upvotes: 1

Related Questions