Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve static files (for CORS / OPTION request) in IIS?

I have an AngularJS application that I am trying to use an AJAX request to pull in a static file from a ASP.NET WebApi2 application running on IIS 8.5. Similar to the example below-

ng-include="http://server/Content/icon.svg"

If I navigate to that URL in the browser, IIS happily serves that file as a static file. However, when I use an AJAX request, Angular attempts an OPTIONS request first, since it is CORS request, and IIS throws a 405 Method Not Allowed.

I have tried adding these headers to the static Content folder in the web site-

Headers

However this made no difference. Also, the IIS server does not have WebDAV installed, which is a thing I have seen around as something that could cause issues.

like image 338
thebringking Avatar asked Sep 13 '25 07:09

thebringking


1 Answers

I had an IIS site with a virtual directory from which I serve my static files.

In order to do CORS from the browser, what I did was configuring the "En tetes de réponses HTTP" of the site, which in english should be something like "HTTP Response Headers".

There I added a new header named "Access-Control-Allow-Origin" with value "*".

From this moment, I can use static files in XHR.

My actual problem was serving OpenStreetMap tiles to a file:///c/xxx/index.html and the error in chrome was

Image from origin 'http://myhost' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

like image 141
Christophe Blin Avatar answered Sep 14 '25 21:09

Christophe Blin