内容类型 'application/json; charset=utf-8' 不是预期的类型 'text/xml; 字符集=utf-8'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17534010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:55:58  来源:igfitidea点击:

the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

jsonwcfasp.net-mvc-4

提问by carlosfigueira

When using firebug, I got this wired error "NetworkError: 415 Cannot process the ...xt/xml; charset=utf-8'. - http://localhost:59899/wsccc1/wscccService.svc/RunTts" in my asp.net mvc 4 project.

使用 firebug 时,我http://localhost:59899/wsccc1/wscccService.svc/RunTts在我的 asp.net mvc 4 项目中收到此有线错误“NetworkError: 415 无法处理 ...xt/xml; charset=utf-8' .-”。

The code:

编码:

<script lang="javascript" type="text/javascript">
    function ttsFunction() {
        serviceUrl = "http://localhost:59899/wsccc1/wscccService.svc/RunTts";
        var data = new Object();
        data.text = $('#speak').val();
        var jsonString = JSON.stringify(data);
        $.ajax({
            type: 'POST',
            url: serviceUrl,
            data: jsonString,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function() { alert('ok')},
            error: function (xhr,status,error) {
                console.log("Status: " + status);
                console.log("Error: " + error);
                console.log("xhr: " + xhr.readyState);
            },
            statusCode: {
                404: function() {
                    console.log('page not found');
                }
            }
        });
    }
</script>

The service code:

服务代码:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class wservice:Iwservice
{
    public string RunTts(string value)
    {
        return value.ToUpper();
    }
}

The interface is:

界面是:

namespace service
{
     [ServiceContract]
     public interface Iwservice
     {
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "RunTts")]
        string RunTts(string text);
     }
}

And web config, I used file-less in WCF.:

和 web 配置,我在 WCF 中使用了无文件。:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment >
       <serviceActivations>
         <add factory="System.ServiceModel.Activation.ServiceHostFactory" 
     relativeAddress="./wsccc1/wscccService.svc" 
     service="service.wservice"/>
      </serviceActivations>
    </serviceHostingEnvironment>
   <behaviors>
     <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
     </serviceBehaviors>
   </behaviors>
  </system.serviceModel>
</configuration> 

回答by carlosfigueira

You need to use the WebServiceHostFactory, instead of the regular ServiceHostFactoryin your code. That will configure the endpoint with the appropriate binding (webHttpBinding) and behavior (webHttp) to honor the [WebInvoke]attribute.

您需要在代码中使用WebServiceHostFactory, 而不是常规ServiceHostFactory。这将使用适当的绑定 ( webHttpBinding) 和行为 ( webHttp)配置端点以支持该[WebInvoke]属性。

<serviceHostingEnvironment >
   <serviceActivations>
     <add factory="System.ServiceModel.Activation.WebServiceHostFactory"
          relativeAddress="./wsccc1/wscccService.svc" 
          service="service.wservice"/>
  </serviceActivations>
</serviceHostingEnvironment>

回答by Oranit Dar

Go to your web.config endpoint tag and check if bindingConfigurationis properly present or not and also make sure the address is spelled correctly.

转到您的 web.config 端点标记并检查是否bindingConfiguration正确存在,并确保地址拼写正确。

<endpoint address="/YourName".../>