You need to enable CORS in Web Api . A simpler and preferred way to enable CORS around the world is to add the following to web.config
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> </customHeaders> </httpProtocol> </system.webServer>
Please note that all methods are set individually, instead of using * . This is due to the fact that when using * an error appears.
You can also enable CORS by code.
Update
The following NuGet package is required: Microsoft.AspNet.WebApi.Cors .
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.EnableCors();
You can then use the [EnableCors] attribute in actions or controllers such as
[EnableCors(origins: "http://www.example.com", headers: "*", methods: "*")]
Or you can register it worldwide
public static class WebApiConfig { public static void Register(HttpConfiguration config) { var cors = new EnableCorsAttribute("http://www.example.com", "*", "*"); config.EnableCors(cors);
You also need to handle Options preview requests with HTTP OPTIONS requests.
Web API should respond to the Options request to confirm that it is indeed configured to support CORS .
To handle this, all you have to do is send a blank answer. You can do this inside your actions, or you can do it globally as follows:
# Global.asax.cs protected void Application_BeginRequest() { if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS") { Response.Flush(); } }
This additional check has been added to ensure that older APIs that were designed to accept only GET and POST requests will not be used. Imagine that you sent a DELETE request to the API when this verb did not exist. The result is unpredictable, and the results can be dangerous.