7

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

编码:

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

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

2 回答 2

11

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

<serviceHostingEnvironment >
   <serviceActivations>
     <add factory="System.ServiceModel.Activation.WebServiceHostFactory"
          relativeAddress="./wsccc1/wscccService.svc" 
          service="service.wservice"/>
  </serviceActivations>
</serviceHostingEnvironment>
于 2013-07-08T20:00:00.310 回答
4

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

<endpoint address="/YourName".../>
于 2014-10-07T12:54:38.920 回答