[英]Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
I am get the above response when calling a WCF service via ajax json.通过 ajax json 调用 WCF 服务时,我得到了上述响应。 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?我该如何解决?
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
.您的端点不是为网络消费定义的 - 它可能使用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.要通过 jQuery(或一般的其他 web/REST 客户端)使用端点,您需要使用WebHttpBinding
定义一个端点,并将WebHttpBehavior
应用于它。
There are different ways to define the endpoint correctly.有多种方法可以正确定义端点。 The easiest one is to use the WebServiceHostFactory
in your .svc file:最简单的方法是在 .svc 文件中使用WebServiceHostFactory
:
UserService.svc :用户服务.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;对于通过搜索到达这里的任何人:'content type'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' 不是预期的类型 'text/xml; charset=utf-8'字符集=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.当我将“[DataContract]”和“[DataMember]”属性正确应用到我的自定义类时,它得到了解决。
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.这是一个很老的问题,但我想在@carlosfigueira 的回答中添加一些信息。
1. Specifying Factory
in .svc
vs. specifing a <service>
in web.config
1. 在.svc
中指定Factory
与在web.config
中指定<service>
The alternative to specifying a Factory
in the .svc
file is specifying a <service>
in the web.config
file.在.svc
文件中指定Factory
的替代方法是在web.config
文件中指定<service>
。 This is explained in many other answers (eg this one ), so I won't repeat them, but it worth copying here yet another @carlosfigueira answer :这在许多其他答案(例如这个)中都有解释,所以我不会重复它们,但值得在这里复制另一个@carlosfigueira答案:
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.如果您未在 .svc 文件中指定任何工厂,则所有端点都将来自 web.config 文件 - WCF 将尝试查找name
属性与完全限定名称匹配的<system.serviceModel / service>
元素服务等级。 If it doesn't find one, then it will add a default endpoint (usingbasicHttpBinding
, unless you changed the default mapping).如果找不到,那么它将添加一个默认端点(使用basicHttpBinding
,除非您更改了默认映射)。
And to be clear: the fully-qualified name should include the namespace.并且要清楚:完全限定的名称应该包括命名空间。 So if one has the following, it would be MyWebServices.ExampleWebService
:因此,如果有以下内容,那将是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 2. 在 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:当我尝试从 Visual Studio 在 localhost 上运行 ajax 调用时,我遇到了这个问题,并得到了这个问题的标题错误消息,所以我想列出解决它所需的步骤:
Factory
in the .svc
, or specify what's needed in the web.config
.如上所述,要么在.svc
中添加Factory
,要么在web.config
中指定需要的内容。.html
file, and select "View in Browser".在 Visual Studio 中,右键单击您的.html
文件,然后选择“在浏览器中查看”。 Your default browser would open, and if it's Chrome, the url would be, eg, localhost:51667/test.html
(which is actually http://localhost:51667/test.html
).您的默认浏览器将打开,如果是 Chrome,则 URL 将是,例如localhost:51667/test.html
(实际上是http://localhost:51667/test.html
)。In my case the problem was the request method.就我而言,问题是请求方法。 I was trying to send GET request instead of POST request.我试图发送 GET 请求而不是 POST 请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.