Pass a user defined object to ASP.NET Webmethod from jQuery, using JSON

I quickly set up this project and I was able to successfully call my web method, please adjust your code accordingly. Make sure your class property names are the same as the ones that you pass through JavaScript. Webservice public static Contact getContact(Contact cnt) { cnt.name = “Abijeet Patro”; cnt.phone = “Blah Blah”; return cnt; … Read more

WebMethod not being called

Try following fixes for your Ajax request: $.ajax({ type: “post”, url: “Playground.aspx/childBind”, data: “{sendData: ‘” + ID + “‘}”, contentType: “application/json; charset=utf-8”, dataType: “json”, success: function (result) { alert(“successful!” + result.d); } }) Notice changed dataType and data value as a string.

The length of the string exceeds the value set on the maxJsonLength property

JavaScriptSerialzer has a public property named MaxJsonLength according to http://msdn.microsoft.com/en-us/library/system.web.configuration.scriptingjsonserializationsection.maxjsonlength.aspx Now, where you are deserializing your json, use this JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = Int32.MaxValue; //Or any size you want to use, basically int maxValue is 2GB, you shouldn’t need this big json string to deserialize, else you are doing it wrong. myObject … Read more

jQuery AJAX call to an ASP.NET WebMethod

You should really make sure to properly encode this JSON. JSON.stringify is the most reliable method: data: JSON.stringify({ accessCode: code, newURL: url }) This ensures that even if the code and url variables contain some dangerous characters that will break your string concatenations in the resulting JSON at the end everything will be properly encoded. … Read more

Call non-static method in server-side from client-side using JavsScript

You can avoid the static constraint by using a simple .asmx page instead of the codebehind page. 1) Open New Website using the AJAX Enable ASP.NET template (it puts the necessary references in the web.config) 2) SIMPLESERVICE.ASMX – Add a new .asmx web service (I called mine SimpleService.asmx) Notice the [System.Web.Script.Services.ScriptSerive] decoration and that the … Read more

“Invalid JSON primitive” in Ajax processing

Just a guess what does the variable json contain after var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);? If it is a valid json object like {“foo”:”foovalue”, “bar”:”barvalue”} then jQuery might not send it as json data but instead serialize it to foor=foovalue&bar=barvalue thus you get the error “Invalid JSON primitive: foo” Try instead setting the data as string … Read more

Calling a ‘WebMethod’ with jQuery in ASP.NET WebForms

Make sure that you have enabled page methods in your ScriptManager element: <asp:ScriptManager ID=”scm” runat=”server” EnablePageMethods=”true” /> and that you have canceled the default action of the button by returning false inside the onclick handler, otherwise the page performs a full postback and your AJAX call might never have the time to finish. Here’s a … Read more

ASP.NET Calling WebMethod with jQuery AJAX “401 (Unauthorized)”

Problem solved This was driving me crazy. Inside ~/App_Start/RouteConfig.cs change: settings.AutoRedirectMode = RedirectMode.Permanent; To: settings.AutoRedirectMode = RedirectMode.Off; (Or just comment the line) Also if friendly URLs are enabled you need to change url: “ConsultaPedidos.aspx/GetClients”, To: url: ‘<%= ResolveUrl(“ConsultaPedidos.aspx/GetClients”) %>’, Hope this help somebody else