在wcf - ajax中启用cors [英] Enable cors in wcf - ajax

查看:727
本文介绍了在wcf - ajax中启用cors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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-Originvalue =*/>
< add name =Access-Control-Allow-Headersvalue =Content-Type,Accept/>
< add name =Access-Control-Allow-Methodsvalue =POST,GET,OPTIONS/>
< add name =Access-Control-Max-Agevalue =1728000/>
< / customHeaders>
< / httpProtocol>

注意



< hr>

注意! 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();
}
}


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 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>

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();
}
}

这篇关于在wcf - ajax中启用cors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆