如何在asp.net中将数组传递给webservice

时间:2014-03-11 17:08:33

标签: asp.net ajax arrays json web-services

我正在尝试将数组作为ajax中的参数传递但我收到错误。请告诉我如何将数组传递给Web服务。

错误

{"Message":"Type \u0027System.String\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\u0026 convertedList)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.AddItemToList(IList oldList, IList newList, Type elementType, JavaScriptSerializer serializer, Boolean throwOnError)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList\u0026 convertedList)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n   at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

网络服务方法

[WebMethod(Description = "Add Session List")]
    [ScriptMethod(UseHttpGet = false)]
    public stringAddSession(string org, string ins, string brn, string startdate, string enddate, string[] classes, string[] subjects, object[] classsubjects)
    {

       return "abc";
    }

ajax电话

$.ajax({
            type: "POST",
            url: /Services/session/SessionService.asmx/AddSession,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ 'org': OrgID, 'ins': InsID, 'brn': BrnID, 'startdate': StartDate, 'enddate': EndDate, 'classes': Classes, 'subjects': Subjects, 'classsubjects': ClassesWithSubjects };),
            dataType: "json",
            processData: false,
            success: object,
            error: function (err) {
                console.log("AJAX ERROR");
                alert(err.responseText);
            }
        });

这是请求Payload

Json on request PayLoad

1 个答案:

答案 0 :(得分:0)

使用下面的代码片段,您也可以实现同样的目标。

var classes = new Array();
classes.push('calss1');
classes.push('class2');

var data = new Object();
data.org = OrgID; //How to add object
data.classes = classes; //How to add array

.......
.......
data:  JSON.stringify(data);
.......
.......