JP Hellemons JP Hellemons - 5 months ago 38
Javascript Question

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 http://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.

Answer

This is a webserver Problem.

edit

Excuse my premature answer. If it's self hosted add this to Katana's web.config or system.config:

<system.webserver>
 <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
      </customHeaders>
    </httpProtocol>
</system.webserver>

Or

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

What option works for you, depends on how your server is configured. Sorry, I am not really in to all the microsoft stuff..

This article could be useful to you.

Comments