通过Jquery Ajax调用Web服务

时间:2013-10-25 05:44:39

标签: asp.net json web-services jquery

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

{“Message”:“无法将类型为\ u0027System.Int32 \ u0027的对象转换为类型\ 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在System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,Type System.Web上的System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化程序,字符串输入,类型类型,Int32 depthLimit)\ r \ n中的类型,JavaScriptSerializer序列化程序,Boolean throwOnError,Object& convertedObject)\ r \ n。在System.Web.Script.Services上的System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext上下文,JavaScriptSerializer序列化程序)\ r \ n中的Script.Serialization.JavaScriptSerializer.Deserialize [T](字符串输入)\ r \ n。 RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext context)\ r \ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,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>

这是我的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 个答案:

答案 0 :(得分:1)

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