1:Get 的基本传参
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script>
function Demo1() {
$.ajax({
url: "/api/Product",
type: "get",
data: { pName: "abdc" },
success: function (obj) {
alert(obj);
}
})
}
</script>
<input type="button" value="Get方式传递基本参数" onclick="Demo1()" />
public class ProductController : ApiController
{
//get 基本传参
public string GetProName(string pName)
{
return pName;
}
```}
```csharp
2:Get 传一个实体
<script>
function Demo2() {
$.ajax({
url: "/api/Product",
type: "get",
data: { Pid: 1, Pname: "Tom" },
success: function (obj) {
alert(obj);
}
})
}
</script>
<input type="button" value="Get方式传递一个实体" onclick="Demo2()" />
public class ProductController : ApiController
{
//get 传一个实体
public string Getproname([FromUri] Models.Product pro)
{
return pro.PName;
}
}
3:利用HttpGet特性传参
<script>
function Demo3() {
$.ajax({
url: "/api/Product",
type: "get",
data: { Pname: "Jom" },
success: function (obj) {
alert(obj);
}
})
}
</script>
`` <input type="button" value="Get方式使用httpget特性传参" onclick="Demo3()" />
public class ProductController : ApiController
{
//利用特性传参
[HttpGet]
public string ShowName(string Pname)
{
return Pname;
}
}`