jquery-ajax(post)调用c#后台 .

来源:互联网 发布:不属于网络群众路线 编辑:程序博客网 时间:2024/05/01 14:10

1) jquery代码:     

                 $.ajax({
                          type: "POST",   //访问WebService使用Post方式请求
                          contentType: "application/json",
                          url: "UserManager.aspx/EditGroup",
                          data: "{strWebName:'" + strWebName + "',strGroupName:'" + strGroupName + "',strNewUserList:'" + strNewUserList + "'}",
                          dataType: 'json',
                          success: function(result) {//回调函数,result,返回值
                              if (result.d == true) {
                                  alert("设置成功!");
                                  diag.close();
                                  window.location.href = window.location.href;
                              }
                              else {
                                  alert("设置失败!");
                                  diag.close();
                              }
                          }
                      });

2) 后台代码:

/// <summary>
        /// 修改组员
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static bool EditGroup(string strWebName,string strGroupName,string strNewUserList)
        {
            bool flag = false;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(
                    delegate()
                    {
                        Role item = dc.Role.SingleOrDefault(u => u.WebName == strWebName && u.GroupName == strGroupName);
                        if (item != null)
                        {
                            item.Users = strNewUserList;
                            dc.SubmitChanges();
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    });
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }

原创粉丝点击