Even after going through this post wcf REST Services and JQuery Ajax Post: Method not allowed (3)
I cannot get past 405: Method not allowed when attempting to invoke a method on my WCF REST service via the following AJAX call:
$.ajax({
type: "GET",
url: 'http://mydomain.com/service1/service.svc/json/getsupportedagencies',
contentType: "application/json; charset=utf-8",
dataType: "json",
failure: function (msg) {
alert(msg);
},
success: function (agencies) {
alert("success via REST");
$.each(agencies, function (i, a) {
viewModel.agencies.push(new agency(a.Name, a.FullName));
});
}
});
The service interface is defined as follows:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, UriTemplate = "getsupportedagencies")]
AgencyDTO[] GetSupportedAgencies();
// other methods
}
After running into threads on cross-domain issues, I manually added a Global.asax file to the webservice project, also adding the following method method:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin",
"http://localhost:80");
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();
}
}
My service model configuration in web.config looks like this:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="ServiceBehavior" name="Services.Service1">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="basic" binding="basicHttpBinding" contract="Services.Contracts.IService1" />
<endpoint address="json"
behaviorConfiguration="JsonBehavior"
binding="webHttpBinding"
contract="Services.Contracts.IService1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="JsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
My unit test to the service using the following code passes:
[Test]
public void TestGetAgencyForLocation()
{
var client = new WebClient();
var response = client.DownloadString(
"http://mydomain.com/service1/service.svc/json/getsupportedagencies");
Assert.IsTrue(!string.IsNullOrEmpty(response));
var agencies= JsonConvert.DeserializeObject(response);
Assert.IsNotNull(agencies);
}
If I post the url "http://mydomain.com/service1/service.svc/json/getsupportedagencies" in Chrome or IE, I get the correct server response in JSON format.
Yes, with all of this in place, I still get 405: method not allowed.
TIA.