Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

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

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> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...