标准动作,配置文件 web.config
中 <system.webServer>
加上用户头
<httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*"/> <add name="Access-Control-Allow-Headers" value="Content-Type, Access-Control-Allow-Headers, Access-Control-Request-Headers, Access-Control-Request-Method, Authorization, X-Requested-With, User-Agent, Referer, Origin"/> <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/> </customHeaders> </httpProtocol>
遇到 未返回有效
http
状态的障碍。原因是浏览器发送options
请求,被拦截。
配置文件 <system.webServer>
中增加:
<modules runAllManagedModulesForAllRequests="false" > <add name="SpecialMethodModule" type="HiOS_NSDataAPI.SpecialMethodModule"/> </modules> SpecialMethodModule 为 IHttpModule 派生类,用于重写服务响应 SpecialMethodModule 内容如下: public SpecialMethodModule() { } public void Init(HttpApplication app) { app.BeginRequest += new EventHandler(this.BeginRequest); } public void Dispose() { } public void BeginRequest(object resource, EventArgs e) { HttpApplication app = resource as HttpApplication; HttpContext context = app.Context; if (context.Request.HttpMethod.ToUpper() == "OPTIONS") { context.Response.StatusCode = 200; context.Response.End(); } }
问题得到解决,此方法还可以用于服务的权限设置。