将Json对象传递给webmethod,给出内部服务器错误500

时间:2014-05-18 06:47:14

标签: asp.net json

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

这是我的代码behid 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&amp; convertedList)\ r \ n在System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError,Object&amp; convertedObject)\ r \ n at the System System.Web.Script.Serialization.JavaScriptSerializer.Deserialize中的.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError,Object&amp; convertedObject)\ r \ n(JavaScriptSerializer序列化程序,字符串输入, Ť System.Web.Script.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context,JavaScriptSerializer)中的System.Web.Script.Serialization.JavaScriptSerializer.Deserialize [T](字符串输入)\ r \ n的ype类型,Int32 depthLimit)\ r \ n序列化程序)\ 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”

1 个答案:

答案 0 :(得分:0)

你应该传递同名的对象数组

 $.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);
                }
            });