Search code examples
jquerycorsowinkatana

Cors access-control-allow-origin header not present


I have an owin/katana project. So no IIS.

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

and client side this 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);
    }
});

enter image description here

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.

I have seen this similar question https://stackoverflow.com/a/6516634/169714 but there is no AppendHeader method. I thought that the * gave everyone access?

edit will try option 3 from this url: https://researchaholic.com/2015/04/28/how-to-fix-no-access-control-allow-origin-header-in-asp-net-webapi/

edit2 adding the nuget and this line app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); and remove the line with headers.add gives me a 500 error, because the origin was already set.


Solution

  • This is a webserver Problem.

    Add this to your configuration file. It corresponds to apaches Access-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>
    

    This will enable cross origin requests for a Katana/Owin setup.

    This article could be useful too.