Returning Anonymous Types from WebService to asp.net Ajax

If your objects do not have complex types, you can have your WebService return an array of anonymous types and .net serializer will correctly serialize your objects to be easily consumable from JavaScript code. In order for you to return anonymous types from your WebService method simply return an array of objects because you do not know what the actual type is? Below is a simple example that demonstrate how to consume an array of anonymous type from asp.net client side.

image

In the above code, I am making use of PageMethods in contrast to WebService by setting EnablePageMethods to true on the script manager. My GetCustomer method on server side simply returns an array of anonymous types.  On the client side I basically consume those objects by looping through them and displaying the content on the label.

No Comments