65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 jason.stringfy() 方法调用带有复杂参数的页面方法

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013 年 10 月 11 日

CPOL

1分钟阅读

viewsIcon

6915

大多数时候,当我尝试搜索使用 jQuery 执行服务器端静态页面方法时,我遇到了一些简单的示例,这些示例解释了如何

当我尝试搜索使用 jquery 执行服务器端静态页面方法时,我发现的大多数例子都解释了如何调用接受简单类型参数(如字符串和整数)的页面方法。然而,没有一个例子解释了如何将复杂对象发送到页面方法。

考虑这个场景,我有一个页面方法,它接受一个 Employee 类型的参数,并返回相应的 Manager,类似于这样:

[System.Web.Services.WebMethod()]
public static Manager ShowGetmanager(Employee emp)
{
Manager oManager = null;
{
// 一些获取 manager 的逻辑
}
return oManager;
}

我想如何从我的 javascript 中将一个 Employee 类型的参数发送到页面方法?正如我之前解释的那样,我无法找到一种发送这种复杂类型的方法,因为我遇到的所有例子都将参数作为字符串使用。然后,我偶然发现了一种名为 JSON.stringfy() 的方法,这就是我用来将复杂对象发送到服务器页面方法的方式

    function GetData() {
       var DTO = { "emp": { "__type": "ResearchWebApplication.Employee", "Name": "Amit", "ID": 1, "Telephone": null} }
       $.ajax({
           type: "POST",
           contentType: "application/json; charset=utf-8",
           url: "Default.aspx/ShowGetmanager",
           data: JSON.stringify(DTO), 
           dataType: "json",
           success: function(msg) {
           $('div#OutputDiv').append(JSON.stringify(msg));
              alert(msg.d);
          }, error: HandleError
       });

   }

   function HandleError(jqXHR, textStatus, errorThrown) {
       $('div#OutputDiv').append(JSON.stringify(errorThrown));
   }

 

© . All rights reserved.