【问题标题】:Cors access-control-allow-origin header not presentCors access-control-allow-origin 标头不存在
【发布时间】:2016-10-30 21:06:15
【问题描述】:

我有一个 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 无法加载http://xxxx:9000 请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin http://yyy 是不允许访问的。

我见过类似的问题https://stackoverflow.com/a/6516634/169714,但没有AppendHeader 方法。我以为* 让每个人都可以访问?

edit 将从该网址尝试选项 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 错误,因为原点已经设置。

【问题讨论】:

    标签: jquery cors owin katana


    【解决方案1】:

    这是一个网络服务器问题。

    将此添加到您的配置文件中。对应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 也可能有用。

    【讨论】:

    • 它是自托管的。欧文/武士刀
    • system.webserver 不是只有 iis 的吗?将尝试第二部分。 Nuget owin.cors 给了我重复源头的问题。
    • 是的,我认为你第一个是对的, 应该适用于 Katana / Owin 设置
    • 似乎 appsettings、nuget 包和 app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 修复了它。
    • 酷!如果我将此添加到我的答案中以使其更完整,可以吗?如果没有,请随时自行更新。
    猜你喜欢
    • 1970-01-01
    • 2016-05-07
    • 2016-10-30
    • 2019-06-19
    • 2021-05-23
    • 2016-11-06
    • 2017-12-11
    • 2014-08-20
    相关资源
    最近更新 更多