weixin_33675507 2009-04-10 19:27 采纳率: 0%
浏览 48

将JSON对象传递给Web方法

I wanted to shared something I learned today with you all. My question was:

Can you pass a JSON object from JavaScript code to a .NET Page Method? For example:

  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 a JSON object to a Page Method, and it comes across as a Dictionary(Of String, String). So my web method signature looks like this:

<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
  • 写回答

2条回答 默认 最新

  • weixin_33686714 2009-04-11 12:19
    关注

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

    Decorate your WebMethod with [GenerateScriptType(typeof(Task))] then in client side you will be able to create task. then pass it as regular object to your server side method.

    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发