Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

Tags:

c#

ajax

wcf

I am get the above response when calling a WCF service via ajax json. My calling code is:

<script type="text/javascript">
    $(document).ready(function () {
        $.ajax
        ({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "http://localhost:90/WebServices/UserService.svc/Calculate",
            data: "{}",
            timeout: 10000,
            dataType: "json",
            success: function (response) {
                alert(response)
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.statusText);
                alert(thrownError);
            }
        });
    });
</script>

My service is:

[ServiceContract]
public interface IUserService
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json
     )]
    Answer Calculate();
}

[DataContract]
public class Answer
{
    [DataMember]
    public string answer { get; set; }
}

My method is :

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class UserService : IUserService
{
    public Answer Calculate()
    {
        Answer answer = new Answer();
        answer.answer="Hello World";
        return answer;
    }
}

I have been battling with for sometime, I see other people have had the same type problem and I tried all there suggestions but still nothing is working.

Where is the problem? How can I solve it?

like image 987
Mark C Avatar asked Jan 18 '13 16:01

Mark C


2 Answers

I'm guessing here since you didn't show how you defined your endpoint, but I'm pretty sure it's the case. Your endpoint is not defined for web consumption - it's likely using basicHttpBinding. To consume the endpoint via jQuery (or other web/REST clients in general) you need to define an endpoint with the WebHttpBinding, and apply the WebHttpBehavior to it.

There are different ways to define the endpoint correctly. The easiest one is to use the WebServiceHostFactory in your .svc file:

UserService.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.UserService"
                Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
like image 145
carlosfigueira Avatar answered Nov 19 '22 11:11

carlosfigueira


For anyone who lands here by searching: 'content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

A similar error was caused in my case by building and running a service without proper attributes. I got this error message when I tried to update the service reference in my client application. It was resolved when I correctly applied '[DataContract]' and '[DataMember]' attributes to my custom classes.

Edit: This would most likely be applicable if your service was set up and working and then it broke after you edited it.

like image 1
Corey Avatar answered Nov 19 '22 11:11

Corey