简体   繁体   English

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

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

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时,我得到了这个有线错误“NetworkError:415无法处理... xt / xml; charset = utf-8'。 - http://localhost:59899/wsccc1/wscccService.svc/RunTts ”在我的asp中.net mvc 4项目。

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> 

You need to use the WebServiceHostFactory , instead of the regular ServiceHostFactory in your code. 您需要使用WebServiceHostFactory ,而不是代码中的常规ServiceHostFactory That will configure the endpoint with the appropriate binding ( webHttpBinding ) and behavior ( webHttp ) to honor the [WebInvoke] attribute. 这将使用适当的绑定( webHttpBinding )和行为( webHttp )配置端点以遵守[WebInvoke]属性。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 内容类型为application / json; 响应消息的charset = utf-8与绑定的内容类型不匹配(text / xml; charset = utf-8) - The content type application/json; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) Express:从 Content-Type &quot;application/json; charset=utf-8&quot; 中删除 charset=utf-8 - Express: Remove charset=utf-8 from Content-Type "application/json; charset=utf-8" Spring REST:HttpMediaTypeNotSupportedException:内容类型&#39;application / json; charset = UTF-8&#39; - Spring REST: HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' 不支持内容类型 'application/json;charset=UTF-8' - Content type 'application/json;charset=UTF-8' not supported 为什么内容类型标头用于json? “应用程序/ JSON; charset = utf-8“或”application / json“? - Why content type header to use for json? “application/json; charset=utf-8 ” or “application/json”? 如何将mappingJacksonHttpMessageConverter的内容类型从application / json; charset = UTF-8更改为application / json - How to change content type of MappingJacksonHttpMessageConverter from application/json;charset=UTF-8 to application/json 从响应中提取 JSON 作为 ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]} - Extracting JSON from response as ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]} 如何将请求和响应“Content-Type”设置为“application/json;charset=UTF-8”? - How to set Request and response “Content-Type” to “application/json;charset=UTF-8”? Cakephp4 不接受内容类型 application/json; 字符集=utf-8 - Cakephp4 don't accept Content Type application/json; charset=utf-8 org.springframework.web.HttpMediaTypeNotSupportedException:内容类型&#39;application / json; charset = UTF-8&#39;不支持 - org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM