415 Unsupported Media Type Calling WCF Service from $.ajax 415 Unsupported Media Type Calling WCF Service from $.ajax ajax ajax

415 Unsupported Media Type Calling WCF Service from $.ajax


I am no expert in this, in fact I had the same problem (for another reason). However, it seems that WCF services don't inherently support AJAX and therefore you must have the following code in your web.config file to enable it.

<system.serviceModel>    <behaviors>        <endpointBehaviors>            <behavior name="NAMESPACE.AjaxAspNetAjaxBehavior">                <enableWebScript />            </behavior>        </endpointBehaviors>    </behaviors>    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"        multipleSiteBindingsEnabled="true" />    <services>        <service name="NAMESPACE.SERVICECLASS">            <endpoint address="" behaviorConfiguration="NAMESPACE.AjaxAspNetAjaxBehavior"                binding="webHttpBinding" contract="NAMESPACE.SERVICECLASS" />        </service>    </services></system.serviceModel>

and then this in the service class

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Activation;using System.ServiceModel.Web;using System.Text;namespace NAMESPACE{    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]    [ServiceContract(Namespace = "")]    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]    public class SERVICECLASS    {        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)        // To create an operation that returns XML,        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],        //     and include the following line in the operation body:        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";        [OperationContract]        public string DoWork()        {            // Add your operation implementation here            return "Success";        }        // Add more operations here and mark them with [OperationContract]    }}

This is what was generated by VS 2012 when I Added an AJAX enabled WCF service.