2013-07-08 12 views
5

Quando si utilizza firebug, ho ricevuto questo errore cablato "NetworkError: 415 Impossibile elaborare il ... xt/xml; charset = utf-8'. - http://localhost:59899/wsccc1/wscccService.svc/RunTts" nel mio progetto asp.net mvc 4.il tipo di contenuto 'application/json; charset = utf-8 'non era il tipo previsto' text/xml; charset = utf-8 '

Il codice:

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

Il codice di servizio:

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

L'interfaccia è:

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

e web di configurazione, ho usato il 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> 
+0

La classe 'wsservice' nello spazio dei nomi' service'? In caso contrario, non corrisponderà all'attivazione del servizio. – carlosfigueira

+0

Sì, non l'ho digitato nel codice. Quando ho digitato http: // localhost: 59899/wsccc1/wscccService.svc nel browser, funziona. –

+0

Ti manca webHttpBehavior? – Praburaj

risposta

11

È necessario utilizzare WebServiceHostFactory anziché il normale ServiceHostFactory nel codice. Ciò configurerà l'endpoint con il binding appropriato (webHttpBinding) e il comportamento (webHttp) per rispettare l'attributo [WebInvoke].

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

Funziona, grazie. –

+0

quindi nel mio caso, ho lo stesso problema e ho capito solo quando provo a fare chiamate al mio servizio da jQuery. Come mai non ho WebServiceHostFactory, ma ha funzionato bene con lo stesso tipo di calll tramite Fiddler mentre lo uso anche per testare? – PositiveGuy

4

Vai al tag endpoint web.config e verificare se bindingConfiguration è propriamente presente o meno e anche assicurarsi che l'indirizzo è stato digitato correttamente.

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