Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX call to C# method is not returning the data

Tags:

jquery

c#

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?

like image 206
MaxWillmott Avatar asked Dec 15 '25 16:12

MaxWillmott


2 Answers

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

like image 164
John Gibb Avatar answered Dec 17 '25 06:12

John Gibb


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

like image 34
Amar Palsapure Avatar answered Dec 17 '25 06:12

Amar Palsapure



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!