简体   繁体   中英

Sending array of Json objects to Web Method using JQuery Ajax

What should the WebMethod's parameter be called below to get the json array sent from client? I used name , but it didn't work.

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);

This is the jquery ajax to send it to Web Method.

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

And this is the Web Method

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

You have to rewrite your data initialization:

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");
                }

use object parameter type on server side:

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

Edit: The quotes removal is not needed as @Felix Kling pointed out.

You have to simply change your web method to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM