I have in my service four methods: Three of them are GET and the other one is POST. My problem is to call this POST method. When I call this method, I always get 404 Bad Request. If I call this method with no body, works.
My codes:
MyService.svc.cs
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "user/{idUser}/garages")]
public GaragesResource GetGarages(string idUser)
{
[...]
}
[...]
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "test")]
public BaseResource PostChecklist(BaseResource baseResource)
{
[...]
}
}
Web.config
[...]
<system.serviceModel>
<services>
<service name="ChecklistService.MyService">
<endpoint address="" behaviorConfiguration="ChecklistService.ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="ChecklistService.MyService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="ChecklistService.ServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
[...]
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
[...]
BaseResource.cs
namespace ChecklistService.Resources
{
[DataContract]
public class BaseResource
{
[DataMember]
public bool Sucess { get; set; }
[DataMember]
public string Message { get; set; }
public BaseResource()
{
}
}
}
I've read that I don't need to explicit the serialization annotations, but I put it to ensure that this wasn't the problem.
And now, the client call:
var data = {
"Sucess":true,
"Message":"Test"
}
$.ajax({
type: "POST",
url: "http://localhost/Checklist/MyService/test",
data: JSON.stringify(data),
contentType: "text/plain",
dataType: "json",
processData: false,
success: function (data, status, jqXHR) {
alert("success…" + data);
},
error: function (xhr) {
}
});
The contentType is set like that because, as informed by jquery documentation, cross domain calls only accept 3 types of contentType or the method of request would change. So, if I use "application/json", the method of request would be OPTIONS, not POST.
I've been trying to make the data in many different ways and nothing. The only way that works is sending an empty string.
I forgot something? Or I set something wrong? Please help.
Thank you!