A bit about the stuff I've done


Friday, 31 May 2013

Type 'System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is not supported for deserialization of an array.

So I was getting this error when calling a page method from jquery. Google turned up quite a few variations but not the exact string, and the suggestions associated with those variations were not relevant. It turned out that in my case the data packet I was sending in the jquery ajax call was wrong. I had code like this:

MyParam = []; ... $.ajax({ type: 'POST', url: document.location.pathname + '/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); } });

and my page method looked like this:

[WebMethod()] public static void DoSomething(List<s> B) { B.ToString(); }

fairly simple but there are 2 things wrong with this. The first problem was the data - I hadn't named the paramater

so the fixed javascript code looks like this:

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

The second problem was it turns out the .NET javascript seraliser doesn't like List<t> so a quick change to the C# fixed that.

[WebMethod()] public static void DoSomething(S[] B) { B.ToString(); }

So in summary - make sure you pass an object with named parameters as the data argument - not simple the content of the only parameter!

5 comments:

  1. Yes, this is a common question asked during interviews. The tips which you have given to answer interview questions is good. Getting a good job can give a start or good boost to the career, hence interviews should go smoothly.thanks
    C and C++ Training Institute in chennai | C and C++ Training Institute in anna nagar | C and C++ Training Institute in omr | C and C++ Training Institute in porur | C and C++ Training Institute in tambaram | C and C++ Training Institute in velachery

    ReplyDelete
  2. Thanks for Sharing This Article.It is very so much valuable content. . AWS course in Chennai

    ReplyDelete
  3. Great explanation of how System.Collections.Generic collections work in C#. Understanding interfaces like IDictionary and how they structure key–value data is very useful for developers working with scalable applications. Posts like this make complex programming concepts easier to grasp. At the same time, continuously upgrading skills through structured programs like Data Science, Full Stack Development, and Digital Marketing can help learners stay industry-ready.

    ReplyDelete
  4. Great post! Understanding type systems, collections, and generics is crucial for writing efficient and maintainable code. Concepts like dictionaries and strongly-typed collections really help improve performance and reduce errors in software development. Posts like this are very useful for both beginners and experienced programmers looking to strengthen their coding skills. For those also looking to build valuable skills in cloud technologies, you can check out this AWS Cloud Course to enhance your knowledge.

    ReplyDelete