I am get the above response when calling a WCF service via ajax json. My calling code is:
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" %>
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.
This is a pretty old question, but I'd like to add some information upon @carlosfigueira answer.
1. Specifying Factory
in .svc
vs. specifing a <service>
in web.config
The alternative to specifying a Factory
in the .svc
file is specifying a <service>
in the web.config
file. This is explained in many other answers (e.g. this one), so I won't repeat them, but it worth copying here yet another @carlosfigueira answer:
If you don't specify any factory in the .svc file, all the endpoints will come from the web.config file - WCF will try to find a
<system.serviceModel / service>
element whosename
attribute matches the fully-qualified name of the service class. If it doesn't find one, then it will add a default endpoint (usingbasicHttpBinding
, unless you changed the default mapping).
And to be clear: the fully-qualified name should include the namespace. So if one has the following, it would be MyWebServices.ExampleWebService
:
namespace MyWebServices
{
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ExampleWebService
{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
public string DoWork(DoWorkRequest request)
{
return "success!";
}
}
}
2. Getting "Cannot process the message..." when called on localhost
I came to this question when trying to run an ajax call on localhost from Visual Studio, and got the error message which is the title of this question, so I'd like to list the required steps to solve it:
Factory
in the .svc
, or specify what's needed in the web.config
..html
file, and select "View in Browser". Your default browser would open, and if it's Chrome, the url would be, e.g, localhost:51667/test.html
(which is actually http://localhost:51667/test.html
).