1
votes

I'm trying to call Web Service to return object, but it is throwing error as

{"Message":"Cannot convert object of type \u0027System.Int32\u0027 to type \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027","StackTrace":" 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"}

Here is my sample of javascript

<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);
                        }
                    }
                });
            });
</script>

and this is my WebService

[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
                });
}
1

1 Answers

1
votes

The UserGroupID which you pass to the webmethod is not json. Try {"roleID":UserGroupID} and use JSON.stringify() method with it.