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!

 
You shares a lot of useful information about technology. Thank you for sharing this with us and keep sharing more like this.
ReplyDeleteC++ Training in Chennai
C C++ Training in Chennai
core java training in chennai
javascript training institute in chennai
javascript training in chennai
core java training in chennai
core java training
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
ReplyDeleteC 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
Thanks for Sharing This Article.It is very so much valuable content. . AWS course in Chennai
ReplyDelete