1.前端调用通过ajax
<script type="text/javascript">
$(function () {
$("#get").click(function () {
$.ajax({
url: "http://localhost:57231/api/Values",
type: "get",
data:{id:1},
success: function (data) {
alert(data);
}
})
});
$("#post").bind("click", function () {
$.ajax({
url: "http://localhost:57231/api/Values",
type: "post",
data: {name:"小小",age:18},
success: function (data) {
alert(data.Name);
},
error: function () {
}
});
});
$("#update").bind("click", function () {
$.ajax({
url: "http://localhost:57231/api/Values?id="+1,
type: "put",
data: { id:1,name: "小小", age: 18 },
success: function (data) {
alert(data.Name);
},
error: function () {
}
});
});
$("#delete").bind("click", function () {
$.ajax({
url: "http://localhost:57231/api/Values?id="+1,
type: "delete",
data: { id: 1, name: "小小", age: 18 },
success: function (data) {
alert(data.Name);
},
error: function () {
}
});
});
})
</script>
API
public string Get(int id) { return "value"; } // POST api/values public User Post([FromBody]User value) { return value; } // PUT api/values/5 public User Put(int id, [FromBody]User value) { return value; } // DELETE api/values/5 public void Delete(int id) { }
2.后台调用:
(1)HttpClient(略)
(2)WebRequest(略)