调用 Webapi 跨域

52 篇文章 0 订阅
46 篇文章 0 订阅


http://blog.csdn.net/hanjun0612/article/details/51799996


先讲一下,web和client各自调用webapi的post和get实例

Get方式

[csharp]  view plain  copy
  1. [HttpGet]  
  2.         public dynamic Test(string a)  
  3.         {  
  4.             return a+"---";  
  5.         }  

web调用

[html]  view plain  copy
  1. $.ajax({  
  2.                 type: "Get",  
  3.                 url: '../api/MyController/Test',  
  4.                 dataType: "json",  
  5. data:"123",  
  6.                 success: function (d) {  
  7. }  
  8. })  

Client调用

[csharp]  view plain  copy
  1. static async void APIGet(string url)  
  2.         {  
  3.             //创建HttpClient(注意传入HttpClientHandler)  
  4.             var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };  
  5.   
  6.             using (var http = new HttpClient(handler))  
  7.             {  
  8.                 //await异步等待回应  
  9.                 var response = await http.GetAsync(url);  
  10.                 //确保HTTP成功状态值  
  11.                 response.EnsureSuccessStatusCode();  
  12.   
  13.                 //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)  
  14.                 Console.WriteLine(await response.Content.ReadAsStringAsync());  
  15.             }  
  16.         }  




Post方式

[csharp]  view plain  copy
  1. [HttpPost]  
  2.         public dynamic Test([FromBody]string data)  
  3.         {  
  4.             return data+"---";  
  5.         }  

web调用

[html]  view plain  copy
  1. var data = { "": "123" };  
  2. $.ajax({  
  3.                     type: "Post",  
  4.                     url: "../api/MyController/Test",  
  5.                     dataType: "json",  
  6.                     data: data,  
  7.                     success: function (d) {  
  8.                           
  9.                     }  
  10.                 });  

client调用

[csharp]  view plain  copy
  1. public static async Task<string> APIPost(string url, string data)  
  2.         {  
  3.             string result = string.Empty;  
  4.             var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };  
  5.             using (var http = new HttpClient(handler))  
  6.             {  
  7.                 var content = new FormUrlEncodedContent(new Dictionary<stringstring>()         
  8.                 {  
  9.                   {"", data}//键名必须为空  
  10.                  });  
  11.   
  12.                 var response = await http.PostAsync(url, content);  
  13.                 response.EnsureSuccessStatusCode();  
  14.                 result = await response.Content.ReadAsStringAsync();  
  15.             }  
  16.             return result;  
  17.         }  


方法1  配置web.config

对于跨域,比较简单的是配置web.config
<system.webServer> 节点下配置:

[html]  view plain  copy
  1. <httpProtocol>  
  2.       <customHeaders>  
  3.         <add name="Access-Control-Allow-Origin" value="*" />  
  4.         <add name="Access-Control-Allow-Headers" value="*" />  
  5.         <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />  
  6.       </customHeaders>  
  7.     </httpProtocol>  


方法2  设置 CORS

http://www.cnblogs.com/landeanfen/p/5177176.html


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值