无法处理的消息,因为内容类型“应用/ JSON的;字符集= UTF-8'是不是预期的类型“文本/ XML;字符集= UTF-8“ [英] Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

查看:1237
本文介绍了无法处理的消息,因为内容类型“应用/ JSON的;字符集= UTF-8'是不是预期的类型“文本/ XML;字符集= UTF-8“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我呼吁通过AJAX JSON 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?

推荐答案

我猜在这里,因为你没有告诉你如何定义你的终点,但我敢肯定它的情况。您的端点没有为网络消费的定义 - 它可能使用 basicHttpBinding的。消耗通过jQuery(或其他一般的Web / REST客户端)端点您需要定义与的WebHttpBinding ,并应用 WebHttpBehavior <端点/ code>吧。

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.

有不同的方法来正确定义端点。一个最简单的就是使用 WebServiceHostFactory 在.svc文件:

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" %>

这篇关于无法处理的消息,因为内容类型“应用/ JSON的;字符集= UTF-8'是不是预期的类型“文本/ XML;字符集= UTF-8“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆