经过大半天努力,终于完成增删改查了!心情有点小激动!!对于初学者的我来说,一路上都是迷茫,坑!!虽说网上有资料,可动手起来却不易(初学者的我)。(苦逼啊!)

WebService 页面:

/// <summary>
/// TsetWeb 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class TsetWeb : System.Web.Services.WebService
{
TestBll bll = new TestBll(); [WebMethod(Description = "获取所有对象信息")]
public string AllUserJson()
{
return ToJson(bll.GetAllUser());
} [WebMethod(Description = "添加一个对象信息")]
public string SetUserJson(string name ,string phone)
{
return ToJson(bll.SetAddUser(name,phone));
}
[WebMethod(Description = "删除一个对象信息")]
public string DelUserJson(int id)
{
return ToJson(bll.DelUser(id));
}
[WebMethod(Description = "更改一个对象信息")]
public string Update(int id, string name, string phone)
{
Test user = new Test();
user.id = id;
user.name = name;
user.phone = phone;
return ToJson(bll.Update(user));
} //对数据序列化,返回JSON格式
public string ToJson(object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
}

AJAX调用WebService 页面:

<body>
<form id="form1" runat="server">
<div>
<table id="tab">
<tr>
<td>编号</td>
<td>名字</td>
<td>电话</td>
<th>操作</th>
</tr>
</table>
</div>
<input type="button" name="add" id="add" value="添加" />
</form>
<script src="Scripts/jquery-1.7.1.min.js"></script>
<script>
$(function() {
$.ajax({
url: "/TsetWeb.asmx/AllUserJson",
contentType: 'application/json',
dataType: "json",
type: "post",
success: function(data) {
var a = eval("(" + data.d + ")"); //后台返回是字符串,所以转换为json对象
$.each(a, function(index, item) {
var tr = $("<tr/>");
$("<td/>").html(item["id"]).appendTo(tr);
$("<td/>").html(item["name"]).appendTo(tr);
$("<td/>").html(item["phone"]).appendTo(tr);
$("<button id ='d' onclick='del(" + item["id"] + ")'>").html("删除").appendTo(tr);
$("<button id ='u' onclick='updata(" + item["id"] + ")'>").html("更新").appendTo(tr);
tr.appendTo("#tab");
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + "," + textStatus + "," + errorThrown);
}
});
});
$("#add").click(function() {
$.ajax({
url: "/TsetWeb.asmx/SetUserJson",
data: "{name:'李六',phone:'13727713819'}",
contentType: 'application/json',
dataType: "json",
type: "post",
success: function (data) {
var a = eval("(" + data.d + ")"); //后台返回是字符串,所以转换为json对象
alert(a);//返回1表示成功
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + "," + textStatus + "," + errorThrown);
}
});
});
function del(id) {
$.ajax({
url: "/TsetWeb.asmx/DelUserJson",
type: "Post",
data: { "id": id },
dataType: "json",
success: function (data) {
var a = eval("(" + data.d + ")"); //后台返回是字符串,所以转换为json对象
alert(a);//返回1表示成功
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + "," + textStatus + "," + errorThrown);
}
});
} function updata(id) {
$.ajax({
url: "/TsetWeb.asmx/Update",
type: "Post",
data: { "id": id, "name": '九九', "phone": '15927713819' },
dataType: "json",
success: function (data) {
var a = eval("(" + data.d + ")"); //后台返回是字符串,所以转换为json对象
alert(a);//返回1表示成功
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + "," + textStatus + "," + errorThrown);
}
});
}
</script>
</body>

  

WebApi页面:

public class ValuesController : ApiController
{
TestBll bll = new TestBll(); // GET api/values/GetAll()
[HttpGet]
public List<Test> GetAll()
{
return bll.GetAllUser();
}
[HttpPost]
public int PostNew([FromBody]Test user)
{
return bll.SetAddUser(user.name, user.phone);
}
[HttpPost]
public int PostNew(string name ,string phone)
{
return bll.SetAddUser(name, phone);
} [HttpDelete]
public int Delete([FromBody]Test user)
{
return bll.DelUser(user.id);
} [HttpPut]
public int Put([FromBody] Test user)
{
return bll.Update(user);
}
}

AJAX调用WebApi页面:

<div>
<table id="tab">
<tr>
<th>编号</th>
<th>名字</th>
<th>电话</th>
<th>操作</th>
</tr>
</table>
<input type="button" name="add" id="add" value="添加" />
</div>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script>
$(function() {
$.ajax({
url: "api/Values/GetAll",
type: "GET",
dataType: "json",
success: function(data) {
$.each(data, function(index, item) {
var tr = $("<tr/>");
$("<td/>").html(item["id"]).appendTo(tr);
$("<td/>").html(item["name"]).appendTo(tr);
$("<td/>").html(item["phone"]).appendTo(tr);
$("<button id ='d' onclick='del(" + item["id"] + ")'>").html("删除").appendTo(tr);
$("<button id ='u' onclick='updata(" + item["id"] + ")'>").html("更新").appendTo(tr);
tr.appendTo("#tab");
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + "," + textStatus + "," + errorThrown);
}
}); });
$("#add").click(function () {
$.ajax({
url: "api/Values/Put",
type: "Put",
data: {"id":id, "name":'赵七',"phone":''},
dataType: "json",
success: function (data) {
alert(data);//返回1表示成功
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + "," + textStatus + "," + errorThrown);
}
});
});
function del(id) {
$.ajax({
url: "api/Values/Delete",
type: "Delete",
data: { "id": id },
dataType: "json",
success: function (data) {
alert(data);//返回1表示成功
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + "," + textStatus + "," + errorThrown);
}
});
} function updata(id) {
$.ajax({
url: "api/Values/Put",
type: "Put",
data: { "id": id, "name": '黄八', "phone": '' },
dataType: "json",
success: function (data) {
alert(data);//返回1表示成功
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + "," + textStatus + "," + errorThrown);
}
});
}
</script>

最新文章

  1. Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
  2. HTML5学习笔记一 简单学习HTML5
  3. java出错
  4. 【BZOJ】1119: [POI2009]SLO
  5. property_自己编写一个读取Property文件的Util类
  6. How to Write a Spelling Corrector
  7. C语言练习题_北理工的恶龙
  8. 一分钟告诉你究竟DevOps是什么鬼?
  9. Struts2 返回 json 格式数据
  10. FastDFS分布式文件系统客户端安装
  11. 【转】TCP、UDP、RTP(RTCP)区别
  12. 3D数学基础(一)Unity坐标系
  13. 100-days: ten
  14. Dojo框架:误解与现实[转载]
  15. XML之Well-Formed文档规则
  16. rsa加密解密, 非对称加密
  17. 大型NodeJS项目架构与优化
  18. JS中的函数节流throttle详解和优化
  19. 初始docker
  20. java核心技术-多线程基础

热门文章

  1. Java实现 蓝桥杯VIP 算法训练 判断字符位置
  2. Java实现 蓝桥杯VIP 算法训练 数组查找及替换问题
  3. Java实现 蓝桥杯VIP 算法训练 数列
  4. java实现第四届蓝桥杯梅森素数
  5. Linux 文件系统常用命令
  6. Python之Flask框架二
  7. 搞清楚C语言指针
  8. [每日一题2020.06.08]洛谷P1605 DFS
  9. LR字符串处理函数-lr_eval_string
  10. SpringMVC框架搭建流程(完整详细版)