I have an ASP.NET MVC 5 project, and one of the pages has some JavaScript that calls an API on a server that is running an ASP.NET WebApi project. The two projects normally run on different domains, but locally they are just on different ports on localhost
. This JavaScript is making a GET
request, but when I watch the network log an OPTIONS
request is being made instead, and the WebApi returns 405 Method Not Authorized
.
From my research, I know that this is a "preflight" check that is sometimes triggered for AJAX calls, and I know that ASP.NET WebApi doesn't support OPTIONS
by default, so that's why I'm getting the 405. However, the preflight check is supposed to be skipped when certain conditions are met, and those conditions have been met here. See for yourself; the following is the (anonymized) entirety of the JS code for the AJAX request:
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:12345/api/SomeAction?someArg=" + someArg;
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
// [snip], process the result here
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
Why is this simple request triggering a preflight check?