内容类型“应用/ JSON的;字符集= UTF-8'是不是预期的类型“文本/ XML;字符集= UTF-8“ [英] the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

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

问题描述

在使用萤火虫,我得到这个错误有线NetworkError:415无法处理...... XT / xml的;字符集= utf-8' - 的http://本地主机:59899 / wsccc1 / wscccService.svc / RunTts 在我的asp.net的MVC 4项目。

在code:

 <脚本LANG =JavaScript的类型=文/ JavaScript的>
    功能ttsFunction(){
        的serviceUrl =HTTP://本地主机:59899 / wsccc1 / wscccService.svc / RunTts
        VAR数据=新的对象();
        data.text = $('#说话)VAL()。
        变种jsonString = JSON.stringify(数据);
        $阿贾克斯({
            输入:POST,
            网址:的serviceUrl,
            数据:jsonString,
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON,
            成功:函数(){警报('OK')},
            错误:功能(XHR,状态,错误){
                的console.log(状态:+状态);
                的console.log(错误:+误差);
                的console.log(XHR:+ xhr.readyState);
            },
            状态code:{
                404:功能(){
                    的console.log(找不到网页);
                }
            }
        });
    }
< / SCRIPT>

该服务code:

  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)
[ServiceBehavior(IncludeExceptionDetailInFaults =真)]
公共类wservice:Iwservice
{
    公共字符串RunTts(字符串值)
    {
        返回value.ToUpper();
    }
}

该接口是:

 命名空间的服务
{
     [服务合同]
     公共接口Iwservice
     {
        [OperationContract的]
        [WebInvoke(方法=POST,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate =RunTts)]
        字符串RunTts(字符串文本);
     }
}

和web配置,我用的文件少WCF:

 <?XML版本=1.0&GT?;
<结构>
  <&的System.Web GT;
    <编译调试=真targetFramework =4.5/>
    <的httpRuntime targetFramework =4.5/>
  < /system.web>
  < system.serviceModel>
    < serviceHostingEnvironment>
       < serviceActivati​​ons>
         <加上工厂=System.ServiceModel.Activati​​on.ServiceHostFactory
     relativeAddress =./ wsccc1 / wscccService.svc
     服务=service.wservice/>
      < / serviceActivati​​ons>
    < / serviceHostingEnvironment>
   <&行为GT;
     < serviceBehaviors>
        <&行为GT;
          < serviceMetadata httpGetEnabled =真/>
        < /行为>
     < / serviceBehaviors>
   < /行为>
  < /system.serviceModel>
< /结构>


解决方案

您需要使用,而不是常规的 WebServiceHostFactory ServiceHostFactory 在code。这将配置与适当的端点结合(的WebHttpBinding )和行为( webHttp )兑现了 [WebInvoke] 属性。

 < serviceHostingEnvironment>
   < serviceActivati​​ons>
     <加上工厂=System.ServiceModel.Activati​​on.WebServiceHostFactory
          relativeAddress =./ wsccc1 / wscccService.svc
          服务=service.wservice/>
  < / serviceActivati​​ons>
< / serviceHostingEnvironment>

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.

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.:

<?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. That will configure the endpoint with the appropriate binding (webHttpBinding) and behavior (webHttp) to honor the [WebInvoke] attribute.

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

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

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