1

我正在尝试调用 Web 服务来返回对象,但它会抛出错误

{"Message":"无法将类型 \u0027System.Int32\u0027 的对象转换为类型 \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027","StackTrace":" 在 System. Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer 序列化器, Boolean throwOnError, Object& convertObject)\r\n 在 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer 序列化器, Boolean throwOnError, Object&convertedObject)\r\n 在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer 序列化程序, 字符串输入, 类型类型, Int32 depthLimit)\r\n 在 System.Web.Script.Serialization.JavaScriptSerializer .Deserialize[T](字符串输入)\r\n 在 System.Web.Script.Services。RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer 序列化程序)\r\n 在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall( HttpContext 上下文,WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

这是我的 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>

这是我的网络服务

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

1 回答 1

1

您传递给 webmethod的UserGroupID不是 json。尝试 {"roleID":UserGroupID} 并使用 JSON.stringify() 方法。

于 2013-10-25T12:28:52.510 回答