Tip #2 - Display JSON String from Object
Sometimes it is necessary to see i.e. the full result you will get inside the asynchronous callback function to see all properties. The AjaxPro JavaScript includes a method to convert a JavaScript object into a JSON string.
<script type="text/javascript"> function myInvoke() { MyNamespace.MyClass.MyMethod(1, 2, 3, myCallback); } function myCallback(res) { // The following line will display a message box // with the JSON serialized string of the result. alert(AjaxPro.toJSON(res.value)); // To see all properties in the res object simple // use following command: alert(AjaxPro.toJSON(res)); } </script>
For debugging this is very useful. Note: you should always check the res.Error property to see if there is any excption during your request.
On the server-side code you could use following line to get the JavaScript JSON representation of the .NET object:
List<string> list = new List<string> (); list.Add ("Michael"); list.Add ("Tanja"); list.Add ("Marc Julian"); list.Add ("Jan Oliver"); string json = AjaxPro.JavaScriptSerializer.Serialize (list);
The output of the json string will be:
["Michael","Tanja","Marc Julian","Jan Oliver"]
You can use this feature to embedd any data structure already in the Page_Load event to your HTML output (JavaScript variable i.e.).