$.ajax()调用.cs里的方法实现ajax操作示例

一、cs代码(返回一个字符串,方法必须是静态方法,且标识WebMethod

//<summary>

        //获取历史记录表格

        //</summary>

        //<param name="preceptUid"></param>

        //<returns></returns>

        [System.Web.Services.WebMethod]//1、这个命名空间很关键,必须指明

        public static string GetCommentList(string DataLocator)//2static很关键,必须是静态方法

        {

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            EntitySet<WorkItem> workItems;

            if (string.IsNullOrEmpty(DataLocator))

            {

                return null;

            }

            else

            {

                workItems = new WorkFlowBLL().GetCommentInOut(DataLocator);

                for (int i = 0; i < workItems.Count; i++)

                {

                    sb.Append("<div>");

                    WorkItem workItem = workItems[i];

 

                    //处理步骤信息                 

                    sb.Append("<font style='color:#f00;'><b>" + workItem.Alias + "</b></font>&nbsp;");

 

                    //处理人信息

                    sb.Append("【");

                    if ((workItem.AssigneeID != null) &&

                            (workItem.AssigneeID != Guid.Empty) &&

                            (workItem.PartName != workItem.AssigneeName))

                    {

                        sb.Append(string.Format("{0}(代{1}办)", workItem.AssigneeName, workItem.PartName));

                    }

                    else

                    {

                        sb.Append(workItem.PartName);

                    }

                    sb.Append("&nbsp;");

                    if ((workItem.AssigneeID != null) &&

                            (workItem.AssigneeID != Guid.Empty))

                    {

                        sb.Append(workItem.AssigneeDeptName);

                    }

                    else

                    {

                        sb.Append(workItem.PartDeptName);

                    }

                    sb.Append("&nbsp;");

                    if ((workItem.AssigneeID != null) &&

                            (workItem.AssigneeID != Guid.Empty))

                    {

                        sb.Append(workItem.AssigneeRank);

                    }

                    else

                    {

                        sb.Append(workItem.PartRank);

                    }

                    sb.Append("】<br />");

 

                    //时间信息

                    if (workItem.FinishTime != null && workItem.FinishTime.Value != DateTime.Parse("2100-12-31 23:59:59"))

                    {

                        sb.Append(workItem.FinishTime.Value.ToString("yyyy年MM月dd日 HH:mm"));

                    }

                    else if (workItem.ReadTime != null)

                    {

                        sb.Append(workItem.ReadTime.Value.ToString("yyyy年MM月dd日 HH:mm"));

                    }

                    else

                    {

                        sb.Append(workItem.ReceTime.Value.ToString("yyyy年MM月dd日 HH:mm"));

                    }

                    sb.Append("<br />");

 

                    //处理意见

                    sb.Append(workItem.PartComment);

 

                    sb.Append("<br />");

                    sb.Append("<br />");

 

                    sb.Append("</div>");

                }

            }

            sb.Append("<a name='#anchorTO'></a>");

            return sb.ToString();

        }

 

二、html代码(注意格式,使用result.d获取方法返回的值)

$.ajax({

                type: 'POST',

                async: false,//如不设置此属性,请求的数据将可能不是最新的而是上次请求的数据

                contentType: 'application/json',

                url: 'ToDoList.aspx/GetCommentList',

                dataType: 'json',//这段代码格式基本固定

                data: '{"DataLocator":"' + data_locator + '"}',// 1、注意参数传入的格式(引号)

                error: function (err) {

                    $menuDiv.html('出错了,错误信息:' + err + ',请重试!');

                },

                success: function (result) {

                    $menuDiv.html(result.d);//2、用result.d获取cs方法返回的数据

                    window.location = "#anchorTO";

                }

            });

 

posted @ 2017-11-14 20:21  kevin860  阅读(968)  评论(0编辑  收藏  举报