我有一个简单的actionmethod,它返回一些json。它在ajax.example.com上运行。我需要从另一个网站someothersite.com访问这个。

如果我尝试调用它,我会得到预期的…:

Origin http://someothersite.com is not allowed by Access-Control-Allow-Origin.

我知道有两种方法可以解决这个问题:JSONP和创建一个自定义HttpHandler 设置标题。

有没有更简单的办法?

对于一个简单的操作来说,是否不可能定义一个允许起源的列表-或者简单地允许所有人?也许是一个动作过滤器?

最理想的是……

return json(mydata, JsonBehaviour.IDontCareWhoAccessesMe);

当前回答

我正在使用DotNet核心MVC,在与nuget包、Startup.cs、属性和这个地方战斗了几个小时后,我简单地将这个添加到MVC动作中:

Response.Headers.Add("Access-Control-Allow-Origin", "*");

我知道这是相当笨拙的,但这是我所需要的,没有其他想要添加这些头文件。我希望这能帮助到其他人!

其他回答

有时OPTIONS动词也会引起问题

简单: 更新你的网页。配置如下

<system.webServer>
    <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
          <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

用httpGet和httpOptions更新webservice/controller头

// GET api/Master/Sync/?version=12121
        [HttpGet][HttpOptions]
        public dynamic Sync(string version) 
        {

这非常简单,只需将其添加到web.config中

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="http://localhost" />
      <add name="Access-Control-Allow-Headers" value="X-AspNet-Version,X-Powered-By,Date,Server,Accept,Accept-Encoding,Accept-Language,Cache-Control,Connection,Content-Length,Content-Type,Host,Origin,Pragma,Referer,User-Agent" />
      <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
      <add name="Access-Control-Max-Age" value="1000" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

在Origin中输入所有可以访问你的网络服务器的域名, 在header中放入任何ajax HTTP请求可以使用的所有可能的header, 在方法中,把你允许的所有方法放在你的服务器上

的问候:)

我正在使用DotNet核心MVC,在与nuget包、Startup.cs、属性和这个地方战斗了几个小时后,我简单地将这个添加到MVC动作中:

Response.Headers.Add("Access-Control-Allow-Origin", "*");

我知道这是相当笨拙的,但这是我所需要的,没有其他想要添加这些头文件。我希望这能帮助到其他人!

我遇到了一个问题,当请求传入cookie时,浏览器拒绝提供它已经检索到的内容(例如,xhr有它的withCredentials=true),并且站点有Access-Control-Allow-Origin设置为*。(Chrome中的错误是,“当凭据标志为真时,不能在Access-Control-Allow-Origin中使用通配符。”)

基于@jgauffin的回答,我创建了这个,这基本上是一种绕过特定浏览器安全检查的方法,所以买者自负。

public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // We'd normally just use "*" for the allow-origin header, 
        // but Chrome (and perhaps others) won't allow you to use authentication if
        // the header is set to "*".
        // TODO: Check elsewhere to see if the origin is actually on the list of trusted domains.
        var ctx = filterContext.RequestContext.HttpContext;
        var origin = ctx.Request.Headers["Origin"];
        var allowOrigin = !string.IsNullOrWhiteSpace(origin) ? origin : "*";
        ctx.Response.AddHeader("Access-Control-Allow-Origin", allowOrigin);
        ctx.Response.AddHeader("Access-Control-Allow-Headers", "*");
        ctx.Response.AddHeader("Access-Control-Allow-Credentials", "true");
        base.OnActionExecuting(filterContext);
    }
}

本教程非常有用。简单总结一下:

使用Nuget上提供的CORS包:Install-Package Microsoft.AspNet.WebApi.Cors 在WebApiConfig.cs文件中,将config.EnableCors()添加到Register()方法中。 为你需要处理cors的控制器添加一个属性:

[EnableCors(origins: "<origin address in here>", headers: "*", methods: "*")]