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();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With