It's quite frustrating to encounter this specific error in the console. Despite the abundance of similar questions on stackoverflow, I have thoroughly researched and confirmed that CORS is enabled in my Web API 2 web service. Yet, the error persists.
Below is my Web API 2 code snippet:
namespace WebApi.App.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class ServiceController : ApiController
{
[HttpGet]
[Route("GetData")]
public IHttpActionResult GetEmpData(DATAvars theDATA)
{
return Ok("WORKED! " + theDATA);
}
[HttpPost]
[Route("PostData")]
public IHttpActionResult PostEmpData(DATAvars theDATA)
{
return Ok("WORKED! " + theDATA.theID);
}
}
public class DATAvars
{
public string theID { get; set; }
public string empImg { get; set; }
}
}
ALSO
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
</modules>
<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>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
ALSO
namespace WebApi.App
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.MapHttpAttributeRoutes();
config.EnableCors();
}
}
}
ALSO
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin" , "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS" )
{
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods" , "GET, POST" );
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers" , "Content-Type, Accept" );
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
}
Next, here is my AJAX call code (hosted on a different domain):
$.ajax({
type: "POST",
crossDomain: true,
url: "http://dev-blahblah/newWS/PostData",
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/json");
},
data: {
theID: "2135648792",
empImg: "false"
},
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
}
});
This is the error displayed in the console: https://i.sstatic.net/vaANs.png
The Console Network indicates: https://i.sstatic.net/ceBSy.png
Failed to load resource: the server responded with a status of 404 (Not Found) index.html:1 XMLHttpRequest cannot load . Response for preflight has an invalid HTTP status code 404
Comparison between the same request in POSTMAN: https://i.sstatic.net/mwxiT.png https://i.sstatic.net/c1aNo.png https://i.sstatic.net/GZUw9.png https://i.sstatic.net/SuZin.png
I have dedicated significant time trying to resolve this issue with countless Google searches for solutions. While examples can be found, none seem effective.
I would greatly appreciate assistance on resolving this matter and getting it to function correctly with JQUERY AJAX.
-Successfully operates on the same domain in CHROME = WORKS
-Does not operate on a different domain in CHROME = DOES NOT WORK
-Successfully operates on the same domain in IE = WORKS
-Successfully operates on a different domain in IE = WORKS