Click here to Skip to main content
15,885,546 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:

MyService.svc.cs
C#
[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
XML
[...]
<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
C#
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:
JavaScript
var data = {
    "Sucess":true, 
    "Message":"Test"
}

/*var data = { 
    "baseResource": {
        "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) {
		//alert(xhr.responseText);
	}
});


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!
Posted

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.
 
Share this answer
 
v5

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



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