webapi 参数的传递
类别:编程学习 浏览量:1264
时间:2016-6-13 webapi 参数的传递
webapi 参数的传递一、Get传递方式
1、基础类型参数
$.ajax({
type: "get",
url: "localhost:80/api/Charging/GetAllChargingData",
data: { id: 1, name: "Jim", bir: "1988-09-11"},
success: function (data, status) {
if (status == "success") {
$("#li_test").html(data);
}
}
});
[HttpGet]
public string GetAllChargingData(int id, string name)
{
return "ChargingData" + id;
}
2、实体作为参数
可以给方法的参数加上[FromUri],即可直接接收实体参数
$.ajax({
type: "get",
url: "localhost:80/api/Charging/GetByModel",
contentType: "application/json",
data: { ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" },
success: function (data, status) {
if (status == "success") {
$("#li_test").html(data);
}
}
});
public class TB_CHARGING
{
///
/// 主键Id
///
public string ID { get; set; }
///
/// 充电设备名称
///
public string NAME { get; set; }
///
/// 充电设备描述
///
public string DES { get; set; }
///
/// 创建时间
///
public DateTime CREATETIME { get; set; }
}
[HttpGet]
public string GetAllChargingData([FromUri]TB_CHARGING obj)
{
return "ChargingData" + obj.ID;
}
二、Post 传递方式
1、单个基础类型参数
$.ajax({
type: "post",
url: "localhost:80/api/Charging/SaveData",
data: { "": "Jim" },
success: function (data, status) {}
});
[HttpPost]
public bool SaveData([FromBody]string NAME)
{
return true;
}
2、多个基础类型参数
$.ajax({
type:"post",
url:"localhost:80/api/Charging/SaveData",
contentType:'application/json',
data:JSON.stringify({ NAME: "Jim",DES:"备注" }),
success:function (data, status){}
});
[HttpPost]
public object SaveData(dynamic obj)
{
var strName = Convert.ToString(obj.NAME);
return strName;
}
3、单个实体作为参数
$.ajax({
type: "post",
url: "localhost:80/api/Charging/SaveData",
data: { ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" },
success: function (data, status) {}
});
[HttpPost]
public bool SaveData(TB_CHARGING oData)
{
return true;
}
4、实体和基础类型一起作为参数传递
var postdata = { ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" };
$.ajax({
type: "post",
url: "localhost:80/api/Charging/SaveData",
contentType: 'application/json
标签:webapi
您可能感兴趣
- webapi 参数的传递
- 使用Visual Studio为WebAPI生成帮助文档
- javascript dom事件模型(JavaScript WebAPI、DOM、事件和操作元素实例详解)
- 使用Fiddler测试WebApi接口
- 她救了被绑架的他,而这一切竟是一场阴谋...(她救了被绑架的他)
- 冬季养殖这6种阴生植物,方便又好养,你家有么(冬季养殖这6种阴生植物)
- 阴生植物为什么不怕照不到阳光(阴生植物为什么不怕照不到阳光)
- 阴生环境 耐阴地被植物,你知道哪些(阴生环境耐阴地被植物)
- 常见的喜阴植物有哪些 养室内盆栽就在这里选(常见的喜阴植物有哪些)
- 这8种耐阴植物,营造阴生植物花境,也是一个不错的选择(营造阴生植物花境)
热门推荐
- 面试中如何给人留下好印象
- docker垃圾清理(docker磁盘空间清理的解决办法)
- mysqlupdate语句用法(MySQL update set 和 and的区别)
- php服务器有哪些(php服务器的系统详解)
- python一分钟认识条件判断(对python判断ip是否可达的实例详解)
- python基于django的博客系统(利用django+wechat-python-sdk 创建微信服务器接入的方法)
- 香港云服务器怎么那么火(如何选择便宜稳定的香港云服务器?)
- mysql数据库死锁原理(MySQL数据库锁机制原理解析)
- html5的优点是什么
- windowsserver2012设置ftp账户(Windows Server 2012安装ftp服务器图文教程)