I'm writing a quick and dirty little script to log traffic on a static HTML page. I am making a POST to another server where I have the API hosted. The server has CORS enabled (see code below) but I still get a 405.
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, PATCH, DELETE, OPTIONS" />
Here's my JavaScript:
var host = window.location.host;
var hostname = window.location.hostname;
var href = window.location.href;
var port = window.location.port;
var path = window.location.pathname;
var referrer = document.referrer;
var obj = {
Host: host,
HostName: hostname,
Href: href,
Port: port,
Path: path,
Referrer: referrer
};
$.ajax({
type: 'POST',
url: 'http://localhost/system/api/sp/logvisit',
data: obj,
dataType: 'json',
contentType: 'application/json'
});
Here is my controller:
namespace Gs.Api
{
public class SPController : ApiController
{
[Route("api/sp/logvisit")]
[HttpPost]
public void LogVisit(LogVisitRequest request)
{
// do stuff with request
}
}
}
I tried the main answer here Handling CORS Preflight requests to ASP.NET MVC actions which got rid of the 405 but my method never actually got called. I tried several other fixes from various answers here and some got rid of the error but my posted object would be null.