【问题标题】: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-07-08 18:59:47
【问题描述】:

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

代码:

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

服务代码:

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

界面是:

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

还有网络配置,我在 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> 

【问题讨论】:

  • wsservice 类是否在 service 命名空间上?如果不是,则与服务激活不匹配。
  • 是的,我没有在代码中输入。当我在浏览器中输入localhost:59899/wsccc1/wscccService.svc 时,它确实有效。
  • 你缺少 webHttpBehavior 吗?
  • 不在web.config中?我是否必须手动添加它以及如何/在哪里?
  • 是的。而且我相信您还没有定义服务端点。根据您正在访问的内容,您应该为此服务配置 webHttpBinding 并且该端点应该具有在其下定义的 的端点行为。引发此错误的原因可能是您尝试访问 SOAP 端点(默认端点),如 REST 端点。

标签: json wcf asp.net-mvc-4


【解决方案1】:

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

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

【讨论】:

  • 那么在我的情况下,我有同样的问题,我只有在尝试从 jquery 调用我的服务时才得到它。为什么我没有 WebServiceHostFactory 却可以很好地通过 Fiddler 进行相同类型的调用,因为我也使用它来进行测试?
  • var settings = { "async": true, "crossDomain": true, "url": "bschoolapp.azurewebsites.net/Service1.svc", "method": "POST", "headers": { "soapAction" :“tempuri.org/IService1/GetAnswers”,“内容类型”:“应用程序/json”,“缓存控制”:“无缓存”,“邮递员令牌”:“5ea26881-e357-4501-a503-3e907ad1448d”}, "processData": false, "data": "{\"queNumber\":\"5\",\"answer\":\"sample answer\",\"studentID\":\"11111\"}" } $.ajax(settings).done(function (response) { console.log(response); });
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-02
    • 2020-04-25
    • 2019-05-28
    • 2020-03-20
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多