IIS 7.5+ Access-Control-Allow-Origin JS

Spread the love

Similar error: xmlhttprequest cannot load no ‘access-control-allow-origin’

Added in Web.config File:

<system.webServer>
<httpProtocol>
<customHeaders>
<add name=“Access-Control-Allow-Origin” value=”*” />
<add name=“Access-Control-Allow-Headers” value=”Origin, X-Requested-With, Content-Type, Accept” />
<add name=“Access-Control-Allow-Methods” value=”GET, POST, PUT, DELETE, OPTIONS” />
</customHeaders>
</httpProtocol>
</system.webServer>

If it don’t work, use next variant:

1. Install CORS

Open Tools>NuGet Package Manager>Package Manager Console and paste Install-Package Microsoft.AspNet.WebApi.Cors

2. Add option in config file
After installation is complete, add in Register function in file WebApiConfig.cs (App_Start/WebApiConfig.cs) next string


config.EnableCors();

3. Fixed Controller
Add next string in your controller

using System.Web.Http.Cors;

and

[EnableCors(origins: “http://mywebclient.azurewebsites.net”, headers: “*”, methods: “*”)]

Добавить комментарий