Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send JSON to WCF 3.5 using Ajax

Tags:

json

ajax

wcf

I'm having issues 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'.

I think I have a problem with either my contract or web.config. All my research turns up empty. I'll be calling this service from a Web Part using jQuery's $.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!

like image 882
MikeKusold Avatar asked Feb 24 '23 16:02

MikeKusold


1 Answers

Here's a full 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);
    }
});

Notice the usage of webHttpBinding instead of basicHttpBinding (SOAP) in web.config as well as the special WebServiceHostFactory used in the .svc file.

like image 169
Darin Dimitrov Avatar answered Feb 27 '23 07:02

Darin Dimitrov