CORS - ajax POST Request, Handle 405 method not found
Today, when I published WCF service and website on UAT server, I started getting 405 (Method not allowed) error on ajax POST requests. After searching a lot on internet, I finally fixed the issue. I am hoping that my findings will save others time.
We can fix this issue using following different options
- Add following code in WCF global.asax file. Publish the code and deploy it.
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
- If you are still getting same issue then add following line of code in your WCF web.config file
<handlers>
<remove name="OPTIONSVerbHandler" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name = "OPTIONSVerbHandler" path="*." verb="GET,HEAD,POST,DEBUG,DELETE,PUT,PATCH,OPTIONS" />
<add name = "ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,DELETE,PUT,PATCH,OPTIONS" />
<add name = "ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,DELETE,PUT,PATCH,OPTIONS" />
<add name = "ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,DELETE,PUT,PATCH,OPTIONS" />
</handlers>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST" />
<add name="Access-Control-Allow-Headers" value="X-Requested-With, Content-Type" />
</customHeaders>
</httpProtocol>
Comments