This is a migrated thread and some comments may be shown as answers.

Error sending JSON object to AjaxManager via AjaxRequest

2 Answers 232 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Ernst Sognnes
Top achievements
Rank 1
Ernst Sognnes asked on 17 Jul 2009, 01:31 PM
Hi,

I have bumped into a small issue regarding sending a JSON object to AjaxManager from client side javascript.

I'm trying to send this string representation of a JSON object:
{'cmd':'Rebind','data':[{'newid':5},{'newname':'test'}]} 

Client side script calling ajaxRequest() thus:
function refreshView(args) { 
    var ajxmgr_id = '<%= RadAjaxManager.GetCurrent(Page).ClientID %>'
    var oAjxMgr = $find(ajxmgr_id); 
    var jsonArgs = JSON.stringify(args); 
    oAjxMgr.ajaxRequest(jsonArgs); 
 
where "args" is the JSON object.

Error message received when passing this JSON object:
Sys.WebForms.PageRequestManagerServerErrorException: Type System.String not supported for deserializing a matrix.

This is the full error message I receive (excuse the Norwegian :P )
Detaljer om feil på webside 
 
Brukeragent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2) 
Tidsstempel: Fri, 17 Jul 2009 13:10:31 UTC 
 
 
Melding: Sys.WebForms.PageRequestManagerServerErrorException: Typen System.String støttes ikke for deserialisering av en matrise. 
Linje: 6 
Tegn: 62099 
Kode: 0 
URI: http://localhost/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=ctl00_scrmgr_HiddenField&compress=1&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d3.5.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3ann-NO%3a0d787d5c-3903-4814-ad72-296cea810318%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2009.2.701.35%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3ann-NO%3a7e598a31-3beb-49a1-914c-5f530240f0ea%3a16e4e7cd%3a86526ba7%3aed16cbdc%3a11a04f7e%3af7645509%3a24ee1bba%3a1e771326%3aa7e79140%3a4cacbc31%3ae524c98b%3a874f8ea2%3a19620875%3a33108d14%3abd8f85e4%3a596d7b53%3a8674cba1%3aef347303%3ab7778d6c%3ac08e9f8a%3aa51ee93e%3a59462f1%3a63b115ed%3a1569bb5f%3adc7e0bd%3a1c565fc%3ac408cfaa 
 

As you can see the JSON object has a nested array in it ('data':[...]). Are there limitations to the complexity of the JSON strings I can pass to ajaxRequest()?

The problem is circumvented by simplifying the JSON object to a flat structure; i.e.:
{'cmd':'Rebind','newid':5,'newname':'test'

With same client side script above, there are no errors and everything works fine.

Is it possible to pass complex nested JSON objects as strings or can I only send flat structured {"key", "value", ...} type objects?

I'm using the lastes RadControls for ASP.NET Ajax (2009.2.701.35)

regards,
Ernst

2 Answers, 1 is accepted

Sort by
0
Ernst Sognnes
Top achievements
Rank 1
answered on 20 Jul 2009, 01:05 PM
It seems I was able to solve my own confusion :) I'll post my findings here for anyone who are experimenting with passing JSON encoded objects to server-side event handlers, and perhaps saving som hair pulling in the process.

My previous goal was to properly handle this JSON string:
{'cmd':'Rebind','data':[{'newid':5,'newname':'Test'}]} 

It seems I inadvertantly made an error already at this point; making "data" an array instead of an inner object. The inner object "data" will then be parsed in codebehind as an ArrayList.

I changed the JSON object to:
{'cmd':'Rebind','data':{'newid':5,'newname':'Test'}} 


And in codebehind - definition of the AjaxManager OnAjaxRequest handler:
protected void ajxmgr_AjaxRequest(object sender, AjaxRequestEventArgs e) 
    // A label for outputting status/debug info 
    Label lblStatus = Page.FindControl("lblStatus"); 
 
    JavaScriptSerializer jsser = new JavaScriptSerializer(); 
     
    // JSON objects deserialized as Dictionary objects containing [String,Object] pairs 
    Dictionary<String, Object> dictArgs = jsser.Deserialize<Dictionary<String, Object>>(e.Argument); 
 
    String strCommand = (String)dictArgs["cmd"]; 
    // The "data" key will contain a Dictionary of [String, Object] pairs 
    Dictionary<String, Object> dictData = (Dictionary<String, Object>)dictArgs["data"]; 
 
    String strNewID = dictData["newid"].ToString(); // Type is Int32 
    String strNewName = (String)dictData["newname"]; 
     
    // Do stuff with the results... 
    lblStatus.Text = "OK: Inserted \"" + strNewName + "\" into DB with ID " + strNewID; 
     
    // ... 

What's going on at this point is the JSON object can be deserialized with JavaScriptSerializer. JSON objects are recognised as Dictionary objects of [String,Object] pairs. Arrays in JSON objects are of type ArrayList, and nested objects are also Dictionary<String,Object>.

A simple server side exception resulted in the cryptic script error I received in IE 8 (see previous post above) because I attempted to cast to an erroneous type. Firefox doesn't say anything, just nothing happens...

Are there any simple ways of getting the proper server side exceptions with stack trace and the whole shebang when they happen during AJAX requests? This would help alot when debugging applications like this. I can of course always turn AJAX off in the manager, but then I can't debug errors during AjaxRequest handling... Chicken and the egg and all that.. :P

Anyone have any thoughts or solutions to that issue?


regards,
Ernst
0
Accepted
Nikolay Rusev
Telerik team
answered on 23 Jul 2009, 06:24 AM
Hello Ernst,

Indeed you are right in your observations. This due to the fact how JavaScriptSerializer works.
However you extend JavaScriptConverter class and utilize the serialization process to your needs.
You can find the documentation for all javascript serialization related classes below:
http://www.asp.net/AJAX/Documentation/Live/mref/T_System_Web_Script_Serialization_JavaScriptConverter.aspx
http://www.asp.net/AJAX/Documentation/Live/mref/N_System_Web_Script_Serialization.aspx

Best wishes,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Ajax
Asked by
Ernst Sognnes
Top achievements
Rank 1
Answers by
Ernst Sognnes
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or