1

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 "";
}
1
  • 1
    If you tell the server that you are sending JSON, you actually have to send JSON. '{name: "' + toServer + '" }' is not JSON. Maybe do only data: toServer instead. Commented Sep 18, 2014 at 17:50

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Lovely. Worked like a charm!
@Jude: Please note that there is no need to remove the quotation marks from the property names. The only important differences are data: JSON.stringify({ name: employees }) and GetDetails(object name). Although data: JSON.stringify(employees) would suffice as well, there is no need to add an additional level to the object structure.
2

You have to simply change your web method to:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.