Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "user/{idUser}/garages")]
    public GaragesResource GetGarages(string idUser)


    [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "test")]
    public BaseResource PostChecklist(BaseResource baseResource)

    <service name="ChecklistService.MyService">
      <endpoint address="" behaviorConfiguration="ChecklistService.ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="ChecklistService.MyService" />
      <behavior name="ChecklistService.ServiceAspNetAjaxBehavior">
        <webHttp />
      <behavior name="serviceBehavior">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="false" />
    <add binding="basicHttpsBinding" scheme="https" />
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  <modules runAllManagedModulesForAllRequests="true" />
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="*" />

namespace ChecklistService.Resources
    public class BaseResource
        public bool Sucess { get; set; }
        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 = {

/*var data = { 
    "baseResource": {

	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!

1 solution

It seems many changes needed in your code...specially while declaring operation contract. Use: BodyStyle = WebMessageBodyStyle.Bare instead of BodyStyle = WebMessageBodyStyle.Wrapped......and do changes in Web.config and while making AJAX call at client side. Lucky almost same scenario is covered in below articles and demo samples given with those articles are working fine.

Part 1: WCF RESTful service and WebGrid in ASP.NET MVC 5 - Part 1[^]
Part 2: WCF RESTful service and WebGrid in ASP.NET MVC 5 - Part 2[^]

Please compare and modify your code accordingly. Please look closely in everything....(In beginning, I suffered too as I was not noticing small changes) and even after that you get errors, we can discuss further. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900