直接上代码:
1 <html> 2 <head> 3 <meta name="viewport" content="width=device-width" /> 4 <script src='/images/loading.gif' data-original="~/Scripts/jquery-1.8.2.js"></script> 5 <script> 6 function Test() { 7 /* 8 无参数Get 9 10 $.ajax( 11 { 12 url: 'api/values/GetUser', 13 type: 'get', 14 //data:{}, 15 success: function (data) { 16 console.log(JSON.stringify(data)); 17 } 18 }); 19 */ 20 21 /* 22 单个参数Get 23 */ 24 /*$.ajax( 25 { 26 url: 'api/values/GetName', 27 type: 'get', 28 data: {name:"pingdong"}, 29 success: function (data) 30 { 31 console.log(JSON.stringify(data)); 32 } 33 }); 34 */ 35 36 /*Get多个参数Get 37 $.ajax( 38 { 39 url: 'api/values/GetNameAndSex', 40 type: 'get', 41 data: {name:'pingdong',sex:'男'}, 42 success: function (data) 43 { 44 console.log(JSON.stringify(data)); 45 } 46 }); 47 */ 48 49 /*post 无参数 50 $.ajax( 51 { 52 url: 'api/values/PostNoParmer', 53 type: 'post', 54 data: {}, 55 success: function (data) { 56 console.log(JSON.stringify(data)); 57 } 58 } 59 ); 60 */ 61 62 /* 63 post一个参数 64 $.ajax({ 65 url: 'api/values/PostName', 66 type: 'post', 67 data: {"":'pingdong'}, 68 success: function (data) 69 { 70 console.log(JSON.stringify(data)); 71 } 72 }); 73 */ 74 75 76 /* 77 post多个参数 78 $.ajax({ 79 url: 'api/values/PostNameAndSex', 80 type: 'post', 81 data: {name:'pingdong',sex:'男'}, 82 success: function (data) 83 { 84 console.log(JSON.stringify(data)); 85 } 86 }); 87 */ 88 89 /* 90 post json格式的数据(单条) 91 92 var mydata = { name: "pingdong", sex: '男' }; 93 94 $.ajax( 95 { 96 url: 'api/values/PostOneJson', 97 type: 'post', 98 data: JSON.stringify(mydata),//将mydata转化为字符串 99 contentType: 'application/json',//默认情况下会出错。因为ajax提交数据默认请求头中的Content-type为:application/x-www-form-urlencoded(即默认的key=value)。而我们需要提交的是json格式的数据,所以需要指定发送到服务端的数据格式:application/json100 succsess: function (data)101 {102 console.log(data);103 }104 });105 */106 107 /*108 post json格式的数据(多条)109 var mydata = [110 { name: "pingdong", sex: '男' },111 {name:'张三',sex:'男'},112 {name:'李四',sex:'男'},113 ];114 $.ajax(115 {116 url: 'api/values/PostMoreJson',117 type: 'post',118 data: JSON.stringify(mydata),119 contentType: 'application/json',120 success: function (data)121 {122 console.log(JSON.stringify(data));123 }124 });125 */126 127 /*传递复杂json(示例)128 129 var mydata = {130 "productPrice": [131 {132 "priceInfo": [133 {134 "productid": "77",135 "webstileid": "2"136 },137 {138 "productid": "191",139 "webstileid": "3"140 }141 ]142 },143 {144 "priceRegex": [145 {146 "webstileid": "2",147 "tid": "6"148 },149 {150 "webstileid": "3",151 "tid": "7"152 }153 ]154 }155 ]156 };157 console.log(JSON.stringify(mydata));158 $.ajax(159 {160 url: 'api/values/PostMoreAndMoreJson',161 type: 'post',162 data: JSON.stringify(mydata),163 contentType: 'application/json',164 success: function (data)165 {166 console.log(JSON.stringify(data));167 }168 });169 */170 }171 172 /*173 备注;174 contentType:指定请求类型 "application/json"175 dataType:指定输出类型 "176 */177 178 </script>179 <title>Index</title>180 </head>181 <body>182 <div>183 <input type="button" onclick="Test()" value="点我" />184 </div>185 </body>186 </html>
1 using My_WebApiDemo.Common; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Net; 6 using System.Net.Http; 7 using System.Web.Http; 8 using Newtonsoft.Json; 9 10 11 namespace My_WebApiDemo.Controllers 12 { 13 // [Authentication] 14 public class ValuesController : ApiController 15 { 16 17 static List<User> userList=new List<User>{new User{ name="张三",password="132456"}, 18 new User{ name="王五",password="456789"}}; 19 public List<User> GetUser() 20 { 21 return userList; 22 } 23 24 public string GetName(string name) 25 { 26 return string.Format("你好,我叫{0}.", name); 27 } 28 29 public string GetNameAndSex(string name, string sex) 30 { 31 return string.Format("你好,我叫{0},性别:{1}", name,sex); 32 } 33 34 public List<User> PostNoParmer() 35 { 36 return userList; 37 } 38 39 /// <summary> 40 /// 单个参数获取的时候,并不是根据key=value,而是 =value 41 /// </summary> 42 /// <param name="name"></param> 43 /// <returns></returns> 44 public string PostName([FromBody]string name) 45 { 46 return string.Format("你好,我叫{0}.", name); 47 } 48 49 /// <summary> 50 /// 传递两个参数的时候,FromBody只能有一个,所以提供一个对象来获取对应的值 51 /// </summary> 52 /// <returns></returns> 53 public string PostNameAndSex(NameAndSex nameAndsex) 54 { 55 return string.Format("你好,我叫{0}.", nameAndsex.name,nameAndsex.sex); 56 } 57 58 /// <summary> 59 /// 接收json格式的数据(单条) 60 /// </summary> 61 /// <returns></returns> 62 public string PostOneJson(NameAndSex nameAndsex) 63 { 64 65 return string.Format("你好,我叫{0}.", nameAndsex.name, nameAndsex.sex); 66 67 } 68 69 /// <summary> 70 /// 接收json格式的数据(多条) 71 /// </summary> 72 /// <returns></returns> 73 public string PostMoreJson(IList<NameAndSex> nameAndsexList) 74 { 75 string str = ""; 76 foreach (var item in nameAndsexList) 77 { 78 str += string.Format("你好,我叫{0}.", item.name, item.sex);; 79 } 80 return str; 81 } 82 83 84 /// <summary> 85 /// 接收json格式的数据(复杂) 86 /// </summary> 87 /// <returns></returns> 88 public string PostMoreAndMoreJson(Demo demo) 89 { 90 return JsonConvert.SerializeObject(demo); 91 } 92 93 94 } 95 public class NameAndSex 96 { 97 public string name { get; set; } 98 public string sex { get; set; } 99 }100 101 public class Demo102 {103 public IList<productPrice> productPrice { get; set; }104 }105 106 public class productPrice107 {108 public IList<priceInfo> priceInfo { get; set; }109 public IList<priceRegex> priceRegex { get; set; }110 }111 112 public class priceInfo113 {114 public string productid { get; set; }115 public string webstileid { get; set; }116 }117 public class priceRegex118 {119 public string webstileid { get; set; }120 public string tid { get; set; }121 }122 }
原标题:webApi 数据绑定 获取
关键词:web