问 题
I am a beginner in WCF, I have been attempting to enable CORS for a WCF service hosted in my IIS. I have gone through several posts and Stack Overflow questions, and all answers are leading me to different solutions and none of them works.
Can someone explain me how to achive this? I tried creating a global.asax and adding begin_request event to set up the headers, but it changed nothing. This is what I used:
protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); HttpContext.Current.Response.End(); } }Where should I start for this and which is the best way for achieving this?
解决方案
I assume WCF service is up and running.Fix in Web.config .Add below section in system.webServer section.
<httpProtocol> <customHeaders> <add value="*"/> <add value="Content-Type, Accept" /> <add value="POST,GET,OPTIONS" /> <add value="1728000" /> </customHeaders> </httpProtocol>Caution
NOTE! The Access-Control-Allow-Origin setting is set to a value of "*". This will allow all callers to have access. You can specify only your caller.
From your existing implementation it should work.However I have slightly tweaked the code and it works for me.
protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin" , "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS" ) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods" , "GET, POST" ); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept, X-Requested-With"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000" ); HttpContext.Current.Response.End(); } }本文地址:IT屋 » Enable cors in wcf - ajax
问 题
我是WCF的初学者,我一直在尝试为我的IIS中托管的WCF服务启用CORS。我已经经历了几个帖子和Stack Overflow问题,所有的答案都是导致我不同的解决方案,没有一个工作。
有人可以解释我如何解决这个问题吗?我试图创建一个global.asax和添加begin_request事件来设置标头,但它什么也没有改变。
这是我使用的:
protected void Application_BeginRequest(object sender,EventArgs e)
{
HttpContext.Current.Response.AddHeader(“Access-Control-Allow-Origin”,“*”);
if(HttpContext.Current.Request.HttpMethod ==“OPTIONS”)
{
HttpContext.Current.Response.AddHeader(“Cache-Control”,“no-cache”);
HttpContext.Current.Response.AddHeader(“Access-Control-Allow-Methods”,“GET,POST”);
HttpContext.Current.Response.AddHeader(“Access-Control-Allow-Headers”,“Content-Type,Accept”);
HttpContext.Current.Response.AddHeader(“Access-Control-Max-Age”,“1728000”);
HttpContext.Current.Response.End();
}
}
我应该从哪里开始,解决方案
我假设WCF服务已启动并运行。 Web.config >
< httpProtocol>
< customHeaders>
< add name =“Access-Control-Allow-Origin”value =“*”/>
< add name =“Access-Control-Allow-Headers”value =“Content-Type,Accept”/>
< add name =“Access-Control-Allow-Methods”value =“POST,GET,OPTIONS”/>
< add name =“Access-Control-Max-Age”value =“1728000”/>
< / customHeaders>
< / httpProtocol>
注意
注意! Access-Control-Allow-Origin设置设置为“*”的值。这将允许所有呼叫者有权访问。您只能指定呼叫者。
从现有的实现中应该可以工作。但是我稍微调整了代码, 。
protected void Application_BeginRequest(object sender,EventArgs e)
{
HttpContext.Current.Response。 AddHeader(“Access-Control-Allow-Origin”,“*”);
if(HttpContext.Current.Request.HttpMethod ==“OPTIONS”)
{
HttpContext.Current.Response.AddHeader(“Access-Control-Allow-Methods”,“GET,POST “);
HttpContext.Current.Response.AddHeader(“Access-Control-Allow-Headers”,“Authorization,Origin,Content-Type,Accept,X-Requested-With”);
HttpContext.Current.Response.AddHeader(“Access-Control-Max-Age”,“1728000”);
HttpContext.Current.Response.End();
}
}
本文地址:IT屋 » 在wcf - ajax中启用cors