【问题标题】:Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'无法处理消息,因为内容类型为 'application/json; charset=utf-8' 不是预期的类型 'text/xml;字符集=utf-8'
【发布时间】:2013-01-02 10:23:37
【问题描述】:

通过 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>

我的服务是:

[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; }
}

我的方法是:

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

我一直在与之斗争一段时间,我看到其他人也遇到过相同类型的问题,我尝试了所有建议,但仍然没有任何效果。

问题出在哪里?我该如何解决?

【问题讨论】:

  • 嗨,您尝试了哪些建议?

标签: c# ajax wcf


【解决方案1】:

我猜这里是因为你没有展示你是如何定义你的端点的,但我很确定是这样的。您的端点未定义用于 Web 消费 - 它可能使用 basicHttpBinding。要通过 jQuery(或一般的其他 web/REST 客户端)使用端点,您需要使用WebHttpBinding 定义端点,并将WebHttpBehavior 应用于它。

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

UserService.svc

<%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.UserService"
                Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

【讨论】:

    【解决方案2】:

    对于任何通过搜索到达这里的人:'content type 'application/json; charset=utf-8' 不是预期的类型 'text/xml;字符集=utf-8'

    在我的案例中,由于构建和运行没有适当属性的服务而导致了类似的错误。当我尝试在客户端应用程序中更新服务引用时收到此错误消息。当我将“[DataContract]”和“[DataMember]”属性正确应用到我的自定义类时,它得到了解决。

    编辑:如果您的服务已设置并正常工作,但在您编辑后它中断了,这很可能适用。

    【讨论】:

    • 请添加代码示例详细说明。
    【解决方案3】:

    这是一个相当古老的问题,但我想在@carlosfigueira 的回答中添加一些信息。

    1.在.svc 中指定Factory 与在web.config 中指定&lt;service&gt;

    .svc 文件中指定Factory 的替代方法是在web.config 文件中指定&lt;service&gt;。这在许多其他答案中都有解释(例如this one),所以我不会重复它们,但值得在这里复制另一个@carlosfigueira answer

    如果您未在 .svc 文件中指定任何工厂,则所有端点 将来自 web.config 文件 - WCF 将尝试找到一个 &lt;system.serviceModel / service&gt;name 属性匹配的元素 服务类的完全限定名称。如果没有找到, 然后它将添加一个默认端点(使用basicHttpBinding,除非 您更改了默认映射)。

    要明确一点:完全限定名称应该包含命名空间。因此,如果有以下内容,那就是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.在本地主机上调用时得到“无法处理消息...”

    我在尝试从 Visual Studio 在 localhost 上运行 ajax 调用时遇到了这个问题,并得到了这个问题的标题错误消息,所以我想列出解决它所需的步骤:

    1. 如上所述,在.svc 中添加Factory,或在web.config 中指定所需内容。
    2. 在 Visual Studio 中,右键单击您的 .html 文件,然后选择“在浏览器中查看”。您的默认浏览器将打开,如果是 Chrome,则 URL 将是,例如,localhost:51667/test.html(实际上是 http://localhost:51667/test.html)。
    3. 您现在可以触发 ajax 调用,希望您能成功获得 Web 服务响应。
    4. 如果您想调试您的网络服务,那么:
      在 Visual Studio 中,确保将 Web 服务的项目设置为启动项目,然后按 F5。这将启动 WCF 测试客户端窗口,但您可以忽略它并触发您的 ajax 请求。

    【讨论】:

      猜你喜欢
      • 2020-04-25
      • 2019-05-28
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多