I'm trying to allow POST requests from my javascript app hosted at localhost:80 to a WCF REStful service hosted at a different port, but somehow it doesn't work. I've tried adding custom properties to the header, as well as adding it programatically in my service's JSONData
method but I'm still getting '405 Method not allowed' in my response. What is the proper approach here ?
This is my interface :
namespace RestService { public class RestServiceImpl : IRestServiceImpl { #region IRestServiceImpl Members public string JSONData() { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); return "Your POST request"; } #endregion } }
and the service code :
using System.ServiceModel; using System.ServiceModel.Web; using System.Web.Script.Services; namespace RestService { [ServiceContract] public interface IRestServiceImpl { [OperationContract] [ScriptMethod] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "export")] string JSONData(); } }
And finally the config :
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour"> <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web"> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehaviour"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="web"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol> </system.webServer> </configuration>
You can enable CORS on service hosted on IIS or IIS Express by adding following configuration in config file. After adding above configuration you should able to access service in client side codes like JavaScript.
This is a security restriction that prevents requests being made from one origin to another. For example, it will prevent an https:// page hitting an http:// address because the protocol is different. It will stop example.com calling another.com because it is a different domain.
This worked better for me than the Web.config version:
Create a Global.asax
Add this method to the Global.asax.cs
:
using System.Web; namespace StackOverflow { public class Global : System.Web.HttpApplication { 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("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(); } } } }
Ref: http://www.dotnet-tricks.com/Tutorial/wcf/X8QN260412-Calling-Cross-Domain-WCF-Service-using-Jquery.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With