Reputation: 69
<head runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function PassJavascriptObjectArraytoWebmethod() {
var CustomerArr = [];
var Customer = new Object();
Customer.CustomerId = "1";
Customer.Name = 'Rakesh';
Customer.Address = 'Mumbai';
CustomerArr.push(Customer);
var Customer = new Object();
Customer.CustomerId = "2";
Customer.Name = 'Sandesh';
Customer.Address = 'Banglore';
CustomerArr.push(Customer);
var param = JSON.stringify(CustomerArr)
$.ajax({
type: 'POST',
url: 'Default2.aspx/AddCustomer',
data: param,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(result) {
alert(result.d);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<span onclick="javascript:PassJavascriptObjectArraytoWebmethod();">Call Customer</span>
</div>
</form>
</body>
here is my code behid webmethod
[WebMethod]
public static string AddCustomer(Customer[] CustomerArr)
{
return "some result";
}
and here is the exception i get in console
"Message":"Type \u0027System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\u0027 is not supported for deserialization of an array.","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList& convertedList)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"
Upvotes: 0
Views: 1490
Reputation: 11104
you should pass object array with same name
$.ajax({
type: 'POST',
url: 'Default2.aspx/AddCustomer',
data: {CustomerArr:param },
// same name
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(result) {
alert(result.d);
}
});
Upvotes: 0