【发布时间】:2014-07-06 07:48:50
【问题描述】:
<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>
这是我在 webmethod 后面的代码
[WebMethod]
public static string AddCustomer(Customer[] CustomerArr)
{
return "some result";
}
这是我在控制台中遇到的异常
"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 不支持数组的反序列化。","StackTrace":" 在 System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList 列表,类型类型, JavaScriptSerializer 序列化程序, Boolean throwOnError, IList&convertedList)\r\n 在 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer 序列化程序, Boolean throwOnError, Object&convertedObject)\r\n 在 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 序列化器, String 输入, Type type,在t32 depthLimit)\r\n 在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](字符串输入)\r\n 在 System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext 上下文,JavaScriptSerializer 序列化程序)\ r\n 在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType" :"System.InvalidOperationException"
【问题讨论】: