AJax技术

程序员软件开发技术分享社区

字号+ 作者:H5之家 来源:H5之家 2017-01-14 14:02 我要评论( )

在wcf - ajax中启用cors(Enable cors in wcf - ajax) - IT屋-程序员软件开发技术分享社区

问 题

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

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • ASP.NET 2.0 技巧和诀窍以及ASP.NET, IIS7 和ASP.NET AJAX End to End 讲座

    ASP.NET 2.0 技巧和诀窍以及ASP.NET, IIS7 和ASP.NET AJAX End to En

    2016-01-17 13:36

  • C#调试技巧

    C#调试技巧

    2016-01-15 16:02

  • asp.net(c#)网页跳转七种方法小结

    asp.net(c#)网页跳转七种方法小结

    2015-12-17 19:36

  • C#实现简易ajax调用后台方法

    C#实现简易ajax调用后台方法

    2015-10-21 19:52

网友点评
n