使用JQueryAjax将Json对象数组发送到Web方法

Sending array of Json objects to Web Method using JQuery Ajax

本文关键字:Web 方法 数组 JQueryAjax Json 对象 使用      更新时间:2023-09-26

下面应该调用什么WebMethod的参数才能从客户端发送json数组?我使用了name,但它不起作用。

var employees = {
            "accounting": [   // accounting is an array in employees.
                  {
                      "firstName": "",  // First element
                      "lastName": "Doe",
                      "age": 23
                  },
                  {
                      "firstName": "Mary",  // Second Element
                      "lastName": "Smith",
                      "age": 32
                  }
            ], // End "accounting" array.                                  
            "sales": [ // Sales is another array in employees.
                              {
                                  "firstName": "Sally", // First Element
                                  "lastName": "Green",
                                  "age": 27
                              },
                              {
                                  "firstName": "Jim", // Second Element
                                  "lastName": "Galley",
                                  "age": 41
                              }
            ] // End "sales" Array.
        } // End Employees
var toServer = JSON.stringify(employees);

这是jquery ajax将其发送到Web方法。

$("#sendtoServer").click(function () {
            $.ajax({
                type        : "POST",
                url         : "Default.aspx/GetDetails",
                data        : '{name: "' + toServer + '" }',
                contentType : "application/json; charset=utf-8",
                dataType    : "json",
                success     : OnSuccess,
                failure     : function (response) {
                    alert("Wrong");
                }
            });
            function OnSuccess(response) {
                alert("Sent");
            }
        });

这就是Web方法

[System.Web.Services.WebMethod]
public static string GetDetails(string name)
{
     var x=name;
     return "";
}

您必须重写数据初始化:

var employees = {
                    accounting: [   // accounting is an array in employees.
                          {
                              firstName: "",  // First element
                              lastName: "Doe",
                              age: 23
                          },
                          {
                              firstName: "Mary",  // Second Element
                              lastName: "Smith",
                              age: 32
                          }
                    ], // End "accounting" array.                                  
                    sales: [ // Sales is another array in employees.
                                      {
                                          firstName: "Sally", // First Element
                                          lastName: "Green",
                                          age: 27
                                      },
                                      {
                                          firstName: "Jim", // Second Element
                                          lastName: "Galley",
                                          age: 41
                                      }
                    ] // End "sales" Array.
                } // End Employees

                $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetDetails",
                    data: JSON.stringify({ name: employees }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                        alert("Wrong");
                    }
                });
                function OnSuccess(response) {
                    alert("Sent");
                }

在服务器端使用对象参数类型:

[System.Web.Services.WebMethod]
public static string GetDetails(object name)
{
     var x=name;
     return "";
}

编辑:正如@Felix Kling所指出的,不需要删除引号。

您只需将web方法更改为:

[System.Web.Services.WebMethod]
public static string GetDetails(object name)
{
     var x=name;
     return "";
}