I have enabled CORs on a Web API and on Chrome the GET and POST requests both fail but on the MS Edge browser, the GET request works with no issue but when I try a POST request, it fails with two error messages in the console:
The request could not be processed by the server due to invalid syntax.
XMLHttpRequest: Network Error 0x80070005, Access is denied.
The code of the post is a standard Ajax request with cross domain enabled:
$.ajax({
type: "POST",
crossDomain: true,
url: 'http://localhost:50915/api/addjob',
data: JSON.stringify(Job),
contentType: "application/json;charset=utf-8",
success: function (data, status, xhr) {
alert("The result is : " + status + ": " + data);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
And on the Web API side I have installed the CORs Nuget package and added the following to enable the code:
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.EnableCors();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var JsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().FirstOrDefault();
if (JsonFormatter != null)
JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
And the Controller:
[System.Web.Http.HttpPost]
[System.Web.Http.Route("api/addjob")]
[EnableCors(origins: "http://localhost", headers: "*", methods: "*")]
public void AddJob([FromBody] Job job)
{
using (_tmsPortalDb)
{
_tmsPortalDb.Jobs.Add(job);
_tmsPortalDb.SaveChanges();
}
}
The URL's are both localhost, but running on different ports, there are both in the same VS Solution as separate projects. My understanding of the CORs support is that this should solve the issue for localhost debugging. Is there something I've missed?