MaxWillmott
MaxWillmott

Reputation: 2220

AJAX call to C# method is not returning the data

I am calling the following C# method:

[WebMethod(true)]
public static List<ReadUserStructure> LoadFriends()
{        
    List<ReadUserStructure> returner = Friend.ReadAllFriends();        
    return returner;
}

With the following jQuery:

$.ajax({
    type: "POST",
    url: "Main.aspx/LoadFriends",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) { alert(msg.Count.toString()); }
});

I have a break point on the C# method and it hits it. stepping through the method the function works fine and, in my user, creates a list with a count of 2. But this list if not getting returned to the client. Any ideas?

Upvotes: 3

Views: 909

Answers (6)

MaxWillmott
MaxWillmott

Reputation: 2220

Everyone whom mention the serialization that was the problem. I am now using the asp.net Ajax extentions which are very nice and easy for what I want to do.

Thank you all for your time and effort.

Upvotes: 0

Amar Palsapure
Amar Palsapure

Reputation: 9680

Put a debugger before alert, debug in firebug or IE or Visual Studio. Check if you are receiving the object msg. If yes use msg.Length instead of msg.Count, else add error handler, and check error.

 success: function (msg) {
     debugger; 
     alert(msg.Length.toString()); 
 },
 error: function (data) {
 }

Hope this helps

Upvotes: 1

John Gibb
John Gibb

Reputation: 10773

The problem is the list won't have a Count property in javascript. Instead, look for msg.length

Upvotes: 2

praveen
praveen

Reputation: 1

If i am not wrong if you are returning json result you need to mention you are returning a json not sure of the syntax but the server should be specifying i am returning a json result

Upvotes: 0

Scen
Scen

Reputation: 1720

Is ReadUserStructure serializable?

Try it with a list of objects that you know should work, like integers or strings.

Upvotes: 0

Andy Stannard
Andy Stannard

Reputation: 1703

Maybe ReadUserStructure is not serializable?

Have you seen the console output in chrome inspector or firebug as that normaly helps out in these matters.

Upvotes: 0

Related Questions