I'm using CORS with WebAPI without issues. Perhaps I'll just describe what I do. If it won't be relevant I'll remove answer. This works for me though.
Edit: Also note, that with CORS the headers have to come in response.
I'm of course using the OWIN. My Startup.cs looks like:
public static void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.EnableCors(new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS, PUT, DELETE")); WebApiConfig.Register(config); app.UseWebApi(config); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); }
Notice, that I had to explicitly EnableCors on my WebApiConfig. Then of course continue by app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
Then just enable cors on my ApiController class as you do:
[EnableCors("*", "*", "GET,POST")] public class FauxDBController : ApiController { [HttpPost] [AllowAnonymous] public mJSONSQLFAUXResponse XYZ(mJSONSQLFAUX data) { } }
Just to show what NuGet packages I use, here is my packages.config:
<packages> <package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net452" /> <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" /> <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" /> <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" /> <package id="Microsoft.AspNet.WebApi.Cors" version="5.2.3" targetFramework="net452" /> <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" /> <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" /> <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" /> <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" /> <package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" /> <package id="Microsoft.Owin.Cors" version="3.0.1" targetFramework="net452" /> <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net452" /> <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" /> <package id="Owin" version="1.0" targetFramework="net452" /> </packages>
Finally, I don't use jQuery but my angular.js file with ajax routine looks like:
$http.post('http://localhost:59113/api/FauxDB/XYZ', { }).success(function (data, status, headers, config) { // something.. }).error(function (data, status, headers, config) { // something.. });
Hope it helps.