Cors access-control-allow-origin header 不存在

Cors access-control-allow-origin header not present

我有一个 owin/katana 项目。所以没有IIS。

public void Configuration(IAppBuilder app)
{
    app.Run(context =>
    {
        context.Response.ContentType = "application/json";
        context.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); 
        // etc.
    }                

和客户端这个 jQuery:

$.ajax({
    dataType: "json",
    url: labelPrintLoc,
    success: function (msg) {
        console.log(msg);
        if (msg === "Done")
            alert("Printed!");
        else {
            alert("Error, check the log on the server!");
        }
    },
    error: function(a, b, c) {
        console.log(a);
        console.log(b);
        console.log(c);
    }
});

XMLHttpRequest cannot load http://xxxx:9000 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin http://yyy is therfore not allowed access.

我看过这个类似的问题但是没有AppendHeader方法。我以为 * 给了每个人访问权限?

edit 将尝试此 url 中的选项 3:https://researchaholic.com/2015/04/28/how-to-fix-no-access-control-allow-origin-header-in-asp-net-webapi/

edit2 添加 nuget 和这一行 app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 并删除带有 headers.add 的行给我一个 500 错误,因为原点已经设置。

这是网络服务器问题。

将此添加到您的配置文件中。对应apachesAccess-Control-Allow-Origin "*".

<appSettings>
  <add key="cors:Origins" value="*" />
  <add key="cors:Headers" value="*" />
  <add key="cors:Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
</appSettings>

这将为 Katana/Owin 设置启用跨源请求。

This article 也很有用。