Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

415 Unsupported Media Type Calling WCF Service from $.ajax

I am attempting to call a WCF web service from an ASPX page like so:

var payload = {
    applicationKey: 40868578
};

$.ajax({
    url: "/Services/AjaxSupportService.svc/ReNotify",
    type: "POST",
    data: JSON.stringify(payload),
    contentType: "application/json",
    dataType: "json"
});

Doing so results in the web server returning the error 415 Unsupported Media Type. I'm sure this is a configuration issue with the WCF service which is defined as follows:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
void ReNotify(int applicationKey);

There are no entries in the web.config file so assume that the service uses the default configuration.

like image 521
Yuck Avatar asked Jul 13 '12 19:07

Yuck


People also ask

How do I fix HTTP 415 unsupported media type?

Fixing 415 Unsupported Media Type errorsEnsure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.

Is 415 unsupported media type?

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.


1 Answers

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.

like image 57
edhurtig Avatar answered Oct 04 '22 14:10

edhurtig