Paw Uio

Paw Uio

  • NA
  • 54
  • 0

CORS problem making ajax call to a web service

Oct 26 2018 7:20 AM
Hello
 
I have a problem with CORS.
I need to call a web service from 2 domains. In my web.config I added headers:
 
CORS works well, but I don't like * in the Origin. I try do do the same in Application_BeginRequest:
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
   //These headers are handling the "pre-flight" OPTIONS call sent by the browser
   HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:3312");
   HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
   HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Accepts,  Content-Type, Origin, X-My-Header");
   HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "60");
   HttpContext.Current.Response.End();
}
 
My idea was to put request host in the Origin as I'm allowed to have only 1 Origin domain.
 
When I check headers in the browser they looks the same but I have CORS error " The Same Origin Policy disallows reading the remote resource..." What to do to get it working?

Answers (1)