AJAX 增删改查
Posted chen0110
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
以上是关于AJAX 增删改查的主要内容,如果未能解决你的问题,请参考以下文章