You get this error because one object is expected, but your code expects a list of objects. Sometimes it can be a little tricky. To simplify data transfer, you must create a class with properties for the type of object that you want to pass to WebMethod, because for ASP.NET it is easier to parse. For instance:
public class Student { private string _name; private int _id; public string name { get { return _name; } set { _name = value; } } public int id { get { return _id; } set { _id = value; } } }
And then your WebMethod will change to accept a list of student-class objects, for example:
[WebMethod] public static void SetStudentInfo(List<Student> Students) { foreach (Student s in Students) {
Then all you have to do is slightly modify your Ajax call as follows:
function setStudentInfo() { var jsonObjects = [ { id: 1, name: "mike" }, { id: 2, name: "kile" }, { id: 3, name: "brian" }, { id: 1, name: "tom" } ]; $.ajax({ type: "POST", url: "ConfigureManager.aspx/SetStudentInfo", contentType: "application/json; charset=utf-8", dataType: "json", async: false, data: { Students: JSON.stringify(jsonObjects) }, success: function (result) { alert('success'); }, error: function (result) { alert(result.responseText); } }); }
source share