码迷,mamicode.com
首页 > Web开发 > 详细

AJAX 增删改查

时间:2020-02-18 09:18:45      阅读:23      评论:0      收藏:0      [点我收藏+]

标签:info   显示   增删改查   efault   http   fun   添加   name   api   

添加

<script>
    function saveData() {
         $.ajax({
                url: https://localhost:44368/api/UserInfo/Add,
                type: post,
                data: {UName:$("#UName").val(),Pass:$("#Pass").val()},
                dataType: json,
                success: function (data) {
                    if (data > 0) {
                        alert(添加成功);
                        location.href = "/default/index";
                    }
                }
            });
    }
</script>

显示、删除

 <script>
        LoadData();
        function LoadData() {
            $.ajax({
                url: https://localhost:44368/api/UserInfo/GetAll,
                type: get,
                data: {},
                dataType: json,
                success: function (data) {
                    $("#tb").empty();
                    $(data).each(function () {
                        $("#tb").append(<tr><td> + this.UId + </td><td> + this.UName + </td><td> + this.Pass + </td><td><input  type="button" value="修改" onclick="Edit( + this.UId + )"/><input  type="button" value="删除" onclick="Del(+this.UId+)"/></td></tr>);
                    });
                }
            });
        }

        function Edit(Id) {
            location.href = "/Default/Edit/" + Id;
        }

        function Del(Id) {
            if (confirm("确认删除么?")) {
                 $.ajax({
                url: https://localhost:44368/api/UserInfo/Remove/+Id,
                type: post,
                data: {},
                dataType: json,
                success: function (data) {
                    if (data > 0) {
                        alert(删除成功);
                        LoadData();//刷新页面
                    }
                }
            });
            }
        }
    </script>
}

 

修改

  <script>
    var UId = @ViewData["UId"];
    LoadData();
    function saveData() {
         $.ajax({
                url: https://localhost:44368/api/UserInfo/Edit,
                type: post,
                data: {UId:$("#UId").val(),UName:$("#UName").val(),Pass:$("#Pass").val()},
                dataType: json,
                success: function (data) {
                    if (data > 0) {
                        alert(修改成功);
                        location.href = "/default/index";
                    }
                }
            });
        }

    function LoadData() {
            $.ajax({
                url: https://localhost:44368/api/UserInfo/GetUserById/+UId,
                type: get,
                data: {},
                dataType: json,
                success: function (data) {
                    $("#UId").val(data.UId);
                    $("#UName").val(data.UName);
                    $("#Pass").val(data.Pass);
                }
            });
        }       
    </script>

 

AJAX 增删改查

标签:info   显示   增删改查   efault   http   fun   添加   name   api   

原文地址:https://www.cnblogs.com/chen0110/p/12324733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!