3

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?

5
  • 1
    msg.Count.toString() <- This looks like C#, but it needs to be Javascript. Please add the results of alert(msg); to your question. Commented Jan 12, 2012 at 16:12
  • Awesome, that displayed [object Object]. So I presume msg is the list? Commented Jan 12, 2012 at 16:21
  • John Gibb has one part of the right idea. a List in C# will be downgraded to an array in Javascript. Commented Jan 12, 2012 at 16:26
  • 1
    In chrome, open the inspector by using ctrl-shift-i (cmd-opt-i on a mac), and switch to the network tab. Then, reload the page and cause the ajax call to happen. You'll be able to inspect the results very clearly and see what's going on from here. Commented Jan 12, 2012 at 16:30
  • I am looping through the array with the normal for loop and trying to do the following: alert(msg[i].UserName); Username is a property of the ReadUserStructure. but this loop is being skipped (in firebug). Commented Jan 12, 2012 at 16:32

6 Answers 6

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Maybe ReadUserStructure is not serializable?

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

3 Comments

Can you explain what you mean by serializable please?
Wikipedia says 'In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and "resurrected" later in the same or another computer environment' But in this case it means it can be transported as JSON.
I would create a Viewmodel (a simple class with proprties for all of the data that is required for the view) and populated it with the data in ReadUserStructure using somthing like AutoMapper. First maybe create the ViewModel create a few instances populated with test data and put them in a list. Then send that over from the webmethod to see if thats the issue.
0

Is ReadUserStructure serializable?

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.