WebSerive Call Through Jquery Ajax
我正在尝试调用Web Service来返回对象,但它正在抛出错误
{"Message":"无法将类型为 u0027System.Int32 u0027的对象转换为类型 u0027System.Collections.Generic.IDictionary`2 [System.String,System.Object] u0027","StackTrace":"at System。 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,Type type,JavaScriptSerializer serializer)中的Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError,Object&convertedObject) r n System.Web.Script.Serialization.JavaScriptSerializer中的System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化程序,字符串输入,类型类型,Int32 depthLimit) r n中的,布尔值throwOnError,Object和convertedObject) r n 。在System.Web.Script.Script.Services.RestHandler.GetRawParams(WebServiceMethodData)的System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext上下文,JavaScriptSerializer序列化程序) r n中反序列化[T](字符串输入) r n methodData ,HttpContext context) r n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
这是我的javascript示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <script type="text/javascript"> $(function () { $('#<%=ddlUserGroup.ClientID %>').change(function () { UserGroupID = $('#<%= ddlUserGroup.ClientID %>').val(); $.ajax({ url:"WebService.asmx/GetMenu", data: UserGroupID, dataType:"json", type:"POST", contentType:"application/json; charset=utf-8", success: function (result) { var rootNode = $("#tree").dynatree("getRoot"); rootNode.removeChildren(); rootNode.addChild($.parseJSON(resp.Data)); }, error: function (jqXHR, textStatus, errorThrown) { var loginUrl = jqXHR.getResponseHeader("loginUrl"); if (jqXHR.status == 502) { window.location = loginUrl; } else { alert('error' + errorThrown); } } }); }); |
这是我的WebService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | [ScriptMethod(ResponseFormat = ResponseFormat.Json)] [WebMethod] public object GetMenu(int roleID) { var menuItems = new TblUserGroupMenuService().GetMenuByUserGroupID(Common.ToInt32(roleID)); List<TblmenuInfo> list = new List<TblmenuInfo>(); foreach (DataRow dr in menuItems.Rows) { list.Add(new TblmenuInfo { Id = Common.ToInt32(dr["ID"]), Label = dr["Name"].ToString(), // Url = dr["Url"].ToString(), // Iconurl = dr["IconURL"].ToString(), Parentid = Common.ToNInt32(dr["ParentID"]), //Order = Common.ToInt32(dr["DisplayOrder"]) }); } return list.AsHierarchy(o => o.Id, parent => parent.Parentid, orderBy => orderBy.Order).AsExtTreeNodes((o, children) => new ExtTreeNode<int> { Key = o.Id, Title = o.Label, Select = (o.Order < 9999), IsFolder = children > 0 }); } |
传递给webmethod的UserGroupID不是json。 尝试{"roleID":UserGroupID}并使用JSON.stringify()方法。