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>