Musa
Musa

Reputation: 315

webservice in jQuery returns collection type

I have an ASP.NET WebService that returns an object of List

public class Students
{
    public string StudentName { get; set; }
    public int Age { get; set; }
}

I am accessing this webservice using this jQuery code

$.ajax({
    type: "POST",
    url: "/Students.asmx/GetStudents",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        $("#myDiv").html(msg.d);
    }
});

But all I get is Object object.

How can I get the data in that object?

Upvotes: 1

Views: 204

Answers (1)

juan
juan

Reputation: 81902

Everything is [Object object] in jquery (when you inspect a jQuery object).

You are actually getting an array of Student objects; you can iterate through the results like this

for (x = 0; x < msg.length; x++) {
    alert(msg[x].StudentName);
}

Upvotes: 1

Related Questions