zoukankan      html  css  js  c++  java
  • AJAX 增删改查

    添加

    <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>
  • 相关阅读:
    反射
    java 验证码识别
    Spring boot + mybatis + orcale
    JVM内存模型及垃圾回收的研究总结
    Java的Array和ArrayList
    Java中最常见的十道面试题
    session和cookie
    Hibernate的load()和get()区别
    ajax跨域获取网站json数据
    对于Spring的IOc和DI的理解
  • 原文地址:https://www.cnblogs.com/chen0110/p/12324733.html
Copyright © 2011-2022 走看看