简体   繁体   English

无法处理消息,因为内容类型为 'application/json; charset=utf-8' 不是预期的类型 'text/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'

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 whose name 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 (using basicHttpBinding , 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 调用时,我遇到了这个问题,并得到了这个问题的标题错误消息,所以我想列出解决它所需的步骤:

  1. As explained above, either add a Factory in the .svc , or specify what's needed in the web.config .如上所述,要么在.svc中添加Factory ,要么在web.config中指定需要的内容。
  2. In Visual Studio, right-click your .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 )。
  3. You can now fire your ajax call, and hopefully, you'd get your web service response successfully.您现在可以触发您的 ajax 调用,并希望您能成功获得 Web 服务响应。
  4. If you'd like to debug your web service, then:如果您想调试您的网络服务,那么:
    In Visual Studio, make sure that the web service's project is set as the Startup Project, and hit F5.在 Visual Studio 中,确保将 Web 服务的项目设置为启动项目,然后按 F5。 This would start WCF Test Client window, but you can just ignore it and fire your ajax request.这将启动 WCF 测试客户端窗口,但您可以忽略它并触发您的 ajax 请求。

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.

相关问题 HTTP 415 无法处理消息,因为内容类型为“application/json; charset=utf-8&#39; 不是预期的类型 &#39;text/xml; 字符集=utf-8&#39; - HTTP 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' HTTP / 1.1 415无法处理消息,因为内容类型为&#39;application / json; charset = utf-8&#39;不是预期的类型&#39;text / xml; 字符集= UTF-8&#39; - HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' 无法处理该消息,因为内容类型“ application / xml”不是预期的类型“ application / soap + xml”; 字符集= utf-8&#39; - Cannot process the message because the content type 'application / xml' was not the expected type 'application / soap + xml; charset = utf-8 ' 响应消息的内容类型application / xml; charset = utf-8与绑定的内容类型不匹配(text / xml; charset = utf-8) - The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) 响应消息的内容类型 application/xml;charset=utf-8 与绑定的内容类型(text/xml; charset=utf-8)不匹配,WCF - The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8), WCF 内容类型 text/xml; 响应消息的 charset=&quot;utf-8&quot; 与绑定的内容类型不匹配 (text/xml; charset=utf-8) - The content type text/xml; charset=“utf-8” of the response message does not match the content type of the binding (text/xml; charset=utf-8) 内容类型text / html; charset =响应消息的UTF-8与绑定的内容类型不匹配(text / xml; charset = utf-8) - The content type text/html; charset=UTF-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) WCF成员资格提供程序引发错误:内容类型&#39;application / json; charset = utf-8&#39;不是预期的类型&#39;application / soap + xml; 字符集= UTF-8&#39; - WCF Membership Provider throws error: content type 'application/json; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8' WCF SOAP服务无法处理该消息,因为它发送多部分消息并且需要&#39;text / xml; charset = utf-8&#39; - WCF SOAP service cannot process the message because it sends multipart message and expects 'text/xml; charset=utf-8' 响应消息的内容类型文本/纯文本与绑定的内容类型不匹配(文本/ xml; charset = utf-8) - The content type text/plain of the response message does not match the content type of the binding (text/xml; charset=utf-8)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM