标签:
1、用visual studio 2015 建立一个 web api 应用程序。记住这是一个 web api 应用。
2、新建一个web api 。
3、用C#访问,代码如下:[没有问题,返回正确]
var requestJson = JsonConvert.SerializeObject(args); HttpContent httpContent = new StringContent(requestJson); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var httpClient = new HttpClient(); var responseJson = httpClient.PostAsync("http://webml01.dxqas.com/datacenter/api/affiliate/queryproduct", httpContent) .Result.Content.ReadAsStringAsync().Result;
但如果用jquery去调用web api 则会出现:
<Error><Message>The requested resource does not support http method ‘OPTIONS‘.</Message></Error>
解决方法如下:
1、编写一个 web api 的基类如 BaseApi.cs 继承自 ApiController 在BaseApi.cs中添加如下代码
public HttpResponseMessage Options() { return new HttpResponseMessage { StatusCode = HttpStatusCode.OK }; }
2、在 web.config 中<system.webServer> 节点中添加如下配置
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, HEAD" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
</customHeaders>
</httpProtocol>
完成以上两部,再用jquery去调用。则不出再报错。但F12看请求时会发现有两个请求产生。
asp.net mvc 5 web api 关于Requested resource does not support options 问题
标签:
原文地址:http://www.cnblogs.com/msql/p/4817049.html