Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type System.Collections.Generic.IDictionary is not supported for deserialization of an array

This error can occur when calling a page method with a manually constructed JQuery.ajax call.

The de-serialization is done by .NET and not in user code.

javascript:

MyParam = []; 
... 
$.ajax({ type: 'POST', 
         url: 'PageOrService.as?x/DoSomething',
         data: JSON.stringify(MyParam), 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         complete: function (a, b, c, d) { console.log(a, b, c, d); } 
       });

C#

[WebMethod()] 
public static void DoSomething(object ParamName) 
{ 
  ParamName.ToString();
}
like image 745
DJL Avatar asked Nov 04 '25 18:11

DJL


1 Answers

In my instance this turned out to be a bad data packet in the jquery ajax call The data packet is supposed to be an object of key-value pairs, with one key per parameter for the web method.

This is probably obvious if you have multiple parameters on your method, but if you don't it is easy to assume you can just pass the parameter content:

this:

data: JSON.stringify(MyParam), 

should be this:

data: JSON.stringify({ParamName:MyParam}),

Obvious if you know - head scratching if you don't!

See my blog post for more info

like image 53
DJL Avatar answered Nov 07 '25 10:11

DJL



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!