直接上代碼: 1 <html> 2 <head> 3 <meta name="viewport" content="width=device-width" /> 4 <script src="~/Scripts/jquery-1.8.2.js"></script> 5 <script> 6 func
直接上代碼:
1 <html> 2 <head> 3 <meta name="viewport" content="width=device-width" /> 4 <script src="~/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/json 100 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:指定輸出類型 "xml" 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 Demo 102 { 103 public IList<productPrice> productPrice { get; set; } 104 } 105 106 public class productPrice 107 { 108 public IList<priceInfo> priceInfo { get; set; } 109 public IList<priceRegex> priceRegex { get; set; } 110 } 111 112 public class priceInfo 113 { 114 public string productid { get; set; } 115 public string webstileid { get; set; } 116 } 117 public class priceRegex 118 { 119 public string webstileid { get; set; } 120 public string tid { get; set; } 121 } 122 }