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

时间:2014-09-18 17:48:33

标签: javascript jquery asp.net ajax json

下面要调用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);

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

$("#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 "";
}

2 个答案:

答案 0 :(得分:3)

您必须重写数据初始化:

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指出,不需要删除引号。

答案 1 :(得分:2)

您只需将您的网络方法更改为:

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