理解中WebAPI的属性和相关操作 FormBody和 FormUri等(WebAPI 二)
1.FromUri使用
将数据通过url方式传递。我们需要在webapi方法标明,这个参数只接受url中参数的值,
$("#Save").click(function () {
$.ajax({
url: 'http://localhost:21162/api/person/?str1=1&str2=3',
type: 'Get',
dataType: 'json',
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});
在参数比较少情况下、或者Url长度小于256的情况下。可以考虑使用FromUri, 部分游览器会现在url大小,在这种情况下我们就要使用FromBody
2.FromBody
将数据通过post data方式方式传递。我们需要在webapi方法标明接受data参数,传递json参数
$("#Save").click(function () {
$.ajax({
url: 'http://localhost:21162/api/person/',
type: 'Get',
dataType: 'json',
data: { "str1": "1", "str2": "2" },
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});
3.ActionName使用,通过制定Action来调用方法
[ActionName("GetAllPerson")]
public IEnumerable<Person> GetAllPerson([FromUri] string str1, [FromUri] string str2)
{
return new List<Person>() {
new Person() {ID="1", Name="李鸿章ALL",Age=15},
new Person() {ID="2", Name="杨玉红ALL",Age=15} };
}
$("#Save").click(function () {
$.ajax({
url: 'http://localhost:21162/api/person/GetAllPerson?str1=1&str2=3',
type: 'Get',
dataType: 'json',
//data: { "str1": "1", "str2": "2" },
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});
4.NonAction 表示不被外部应用程序调用
5. [AcceptVerbs("GET","POST")]
[ActionName("GetAllPerson")] //指定ActionName
[NonAction] //不被外部应用程序调用
[AcceptVerbs("GET","POST")] //表示既可以Get请求,又可以Post请求
public IEnumerable<Person> GetAllPerson([FromUri] string str1, [FromUri] string str2)
{
return new List<Person>() {
new Person() {ID="1", Name="李鸿章ALL",Age=15},
new Person() {ID="2", Name="杨玉红ALL",Age=15} };
}
理解中WebAPI的属性和相关操作 FormBody和 FormUri等(WebAPI 二)的更多相关文章
- Web API与AJAX:理解FormBody和 FormUri的WebAPI中的属性
这是这一系列文章"与 AJAX 的 Web API".在这一系列我们都解释消耗 Web API rest 风格的服务使用 jQuery ajax() 和其他方法的各种方法.您可以阅 ...
- python三大框架之一flask中cookie和session的相关操作
状态保持 Cookie cookie 是指某些网站为了 辨别 用户身份,进行会话跟踪而储存在用户本地的数据(通常会经过加密),复数形式是 coolies. cookie是由服务器端生成,发送给客户端 ...
- 11G RAC 中 OCR 及Voting Disk 相关操作
一.启动oracle clusterware先决条件:Oracle High Availability Services daemon(OHASD)运行在所有集群节点上1.启动整个Oracle Clu ...
- java-Eclipse中使用JDBC连接数据库及相关操作
准备工作:mysql-connector-java-5.1.6-bin.jar配置 package com.job; import java.sql.Connection; import java.s ...
- Python3中的List列表的相关操作
列表对象内建函数 1. append(obj) 在列表的末尾添加新元素obj.例: >>> a = ['a', 'b', 'c'] >>> a.append('d' ...
- JS中对象的定义及相关操作
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- set-cookie中的SameSite属性
原文:set-cookie中的SameSite属性 再见,CSRF:讲解set-cookie中的SameSite属性 2016-04-14 13:18:42 来源:360安全播报 作者:暗羽喵 阅读: ...
- C# 对类中的保护成员进行写操作(邀请大家拍砖)
假如我有一个类库 Lib,提供一个类 ClassA 对外服务,ClassA 中有若干只读属性 PropA, PropB 等等,外部调用者无法对 ClassA 中的 PropA 和 PropB 进行写操 ...
- 理解特性attribute 和 属性property的区别 及相关DOM操作总结
查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点: ...
随机推荐
- iOS UIWebView 访问https 绕过证书验证的方法
在文件开始实现 allowsAnyHTTPSCertificateForHost 方法 @implementation NSURLRequest (NSURLRequestWithIgnoreSSL ...
- 不用不知道 apply()与call()的强大
在看关于javascript继承的时候 好多地方都用到了apply()和call() 之前在简单编程的时候根本没有遇到过 查阅资料后才发现这两个方法是如此的好用. 下面从几方面来看一下这两个方法: 1 ...
- 我的第一个comet长连接例子
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...
- leiningen安装记录
Leiningen是Clojure项目管理工具Leiningen is the easiest way to use Clojure,官网:http://leiningen.org/ 1:首先下载Le ...
- Go代理,修改标题
- 《how to design programs》第10章表的进一步处理
返回表的函数: 下面是一个求工资的函数: ;; wage : number -> number ;; to compute the total wage (at $12 per hour) ...
- cf471B MUH and Important Things
B. MUH and Important Things time limit per test 1 second memory limit per test 256 megabytes input s ...
- #292 (div.2) D.Drazil and Tiles (贪心+bfs)
Description Drazil created a following problem about putting × tiles into an n × m grid: "The ...
- MySQL Workbench导出数据库
步骤: 1. 打开mysql workbench,进入需要导出的数据库,点击左侧栏的[Management]tab键. 2. 点选要输出的数据库 点击[Data Export] 选在要输出的数据库 选 ...
- Handshakes(思维) 2016(暴力)
Handshakes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...