Pass the JSON object to the web method

I wanted to share what I learned today with all of you. My question is:

Can you pass a JSON object from JavaScript code to a .NET method? For instance:

var task = { Title: $("#titlenew input", $(newTaskRow)).val(), StartDate: $("#startnew input", $(newTaskRow)).val(), EndDate: $("#endnew input", $(newTaskRow)).val(), EstimatedHours: $("#esthrsnew input", $(newTaskRow)).val(), PredecessorsOutlineNumbers: $("#depnew input", $(newTaskRow)).val(), OutlineNumber: $("#ordernew", $(newTaskRow)).text() }; PageMethods.AddTask(task, saveNewTaskCompleted, saveNewTaskFailed); 

And if you can, what type of .NET object should my web method accept?

I found out that yes, you can pass the JSON object to the page method, and it appears as a dictionary (Of String, String). Therefore, my signature on the Internet is as follows:

 <System.Web.Services.WebMethod()> _ Public Shared Sub AddTask(ByVal taskJson As Dictionary(Of String, String)) Dim oTask As New Task() oTask.Title = taskJson("Title") ' all other accesses to the JSON object here End Sub 
+4
source share
2 answers

Check out this article: http://dotnetslackers.com/columns/ajax/ASPNETAjaxWebService.aspx

Decorate your WebMethod with [GenerateScriptType (typeof (Task))], and then on the client side you can create a task. then pass it as a regular object for your server side method.

+4
source

A detailed answer to a similar question indicates that combining JQuery and Json2.stringfy () can be used to send complex types to Server-Side methods.

And on the server side, you will only need to put the required type in the method signature

(for example, foo (MyType obj) {...})

How to send a JSON object to asp.net web service and process the data there?

0
source

All Articles