I am get the above response when calling a WCF service via ajax json. My calling code is:
当通过ajax json调用WCF服务时,我得到上述响应。我的调用代码是:
<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?
问题在哪里?我该怎么解决呢?
2 个解决方案
#1
17
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.
我猜是因为你没有展示你是如何定义你的端点的,但我很确定是这样的。您的端点不是为web消费定义的——它很可能使用basicHttpBinding。要使用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:
UserService.svc:
<%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.UserService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
#2
1
For anyone who lands here by searching: 'content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
对于任何通过搜索登录到这里的人:“内容类型”应用程序/json;charset=utf-8'不是预期的类型'文本/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.
在我的案例中,一个类似的错误是建立和运行一个没有适当属性的服务。当我尝试在我的客户端应用程序中更新服务引用时,我得到了这个错误消息。当我正确地将“[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.
编辑:如果你的服务是建立和工作的,并且在你编辑它之后它就坏了,这很有可能是适用的。
#1
17
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.
我猜是因为你没有展示你是如何定义你的端点的,但我很确定是这样的。您的端点不是为web消费定义的——它很可能使用basicHttpBinding。要使用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:
UserService.svc:
<%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.UserService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
#2
1
For anyone who lands here by searching: 'content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
对于任何通过搜索登录到这里的人:“内容类型”应用程序/json;charset=utf-8'不是预期的类型'文本/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.
在我的案例中,一个类似的错误是建立和运行一个没有适当属性的服务。当我尝试在我的客户端应用程序中更新服务引用时,我得到了这个错误消息。当我正确地将“[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.
编辑:如果你的服务是建立和工作的,并且在你编辑它之后它就坏了,这很有可能是适用的。