Send JSON to WCF 3.5 using Ajax

I'm having trouble passing JSON to the Weight method. I keep getting HTTP/1.1 415 Cannot process the message because the content type 'application/x-www-form-urlencoded; charset=UTF-8' was not the expected type 'text/xml; charset=utf-8'. HTTP/1.1 415 Cannot process the message because the content type 'application/x-www-form-urlencoded; charset=UTF-8' was not the expected type 'text/xml; charset=utf-8'.

I think I have a problem with my contract or with web.config. All my studies become empty. I will call this service from the web part using jQuery $ .ajax.

Interface:

 namespace XXX.SharePoint.WebServices { [ServiceContract] public interface ICalculators { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json )] Single Weight(Single Width, Single Diameter, Single Size, Single Factor); } } 

web.config:

 <?xml version="1.0"?> <configuration> <system.serviceModel> <services> <service behaviorConfiguration="XXX.SharePoint.WebServices.CustomServiceBehaviour" name="XXX.SharePoint.WebServices.Calculators"> <endpoint address="" binding="basicHttpBinding" contract="XXX.SharePoint.WebServices.ICalculators" > <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://moss2010/"></add> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="XXX.SharePoint.WebServices.CustomServiceBehaviour"> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> 

As always, thanks in advance!

+4
source share
2 answers

Here is a complete working example of a WCF service hosted in IIS:

 [ServiceContract] public interface ICalculators { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json )] float Weight(float width, float diameter, float size, float factor); } public class Calculators : ICalculators { public float Weight(float width, float diameter, float size, float factor) { return 10f; } } 

calculators.svc :

 <%@ ServiceHost Language="C#" Debug="true" Service="XXX.SharePoint.WebServices.Calculators" Factory="System.ServiceModel.Activation.WebServiceHostFactory" CodeBehind="Calculators.svc.cs" %> 

web.config:

 <system.serviceModel> <services> <service behaviorConfiguration="XXX.SharePoint.WebServices.CustomServiceBehaviour" name="XXX.SharePoint.WebServices.Calculators"> <endpoint address="" binding="webHttpBinding" contract="XXX.SharePoint.WebServices.ICalculators" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="XXX.SharePoint.WebServices.CustomServiceBehaviour"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> 

Consumption using jQuery in the same ASP.NET application:

 $.ajax({ url: '/calculators.svc/Weight', type: 'POST', contentType: 'application/json', data: JSON.stringify({ Width: 1.2, Diameter: 2.3, Size: 3.4, Factor: 4.5 }), success: function (result) { alert(result.WeightResult); } }); 

Note the use of webHttpBinding instead of webHttpBinding (SOAP) in web.config, as well as the special WebServiceHostFactory used in the .svc file.

+6
source

You are sending a request that uses a content type with forms / url encodings (by default for jQuery.ajax when the value of the data element is an object) that WCF does not support by default. You can either change it to send JSON, as suggested by Darin Dimitrov, change your service operation to take Stream as a parameter and independently analyze the input or expand WCF to support form / encoded data. I sent the sample to another question (in the PEST issue of the RESTful WCF web service ), which does the latter.

In addition, the preview for jQuery support from http://wcf.codeplex.com has code that supports this type of content.

+1
source

All Articles