2017-04-26 2 views
2

Ich habe ein Problem mit dem Aufrufen eines WCF-Dienstes von jquery. Der Fehler, den ich bekomme, istPOST mit jquery zu WCF

415 Kann die Nachricht nicht verarbeiten, weil der Inhaltstyp 'Application/Json; charset = utf-8 'war nicht der erwartete Typ' text/xml; charset = utf-8 '.

Service1.cs

public class Service1 : IService1 
{ 
    public string GetData(int value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 

    public string Authenticate(string data) 
    { 
     return "1"; 
    } 
} 

IService1

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    string GetData(int value); 

    [OperationContract] 
    [WebInvoke(Method = "POST", 
     ResponseFormat = WebMessageFormat.Xml, 
     RequestFormat = WebMessageFormat.Xml, 
     BodyStyle = WebMessageBodyStyle.Bare, 
     UriTemplate = "auth")] 

    string Authenticate(string data); 

} 

app.config

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="TestServiceLibrary.Service1" behaviorConfiguration="ServiceBehaviour"> 
     <!-- Service Endpoints --> 
     <!-- Unless fully qualified, address is relative to base address supplied above --> 
     <endpoint address ="" binding="webHttpBinding" contract="TestServiceLibrary.IService1" behaviorConfiguration="web"> 
      <!-- 
       Upon deployment, the following identity element should be removed or replaced to reflect the 
       identity under which the deployed service runs. If removed, WCF will infer an appropriate identity 
       automatically. 
      --> 
     </endpoint> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehaviour"> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

test.html

<html> 
<head> 
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> 
    <script> 
     $(document).ready(function() { 
      var tests = "test"; 
      $.ajax({ 
       cache: false, 
       type: "POST", 
       async: false, 
       url: "http://localhost/TestService/Service1.svc/auth", 
       data: '{"data": "' + tests + '"}', 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       processData: true, 
       success: function (msg) { 
        alert('success'); 
       } 
      }); 
     }); 


    </script> 
</head> 
<body> 
</body> 
</html> 

Ich versuchte auch ohne Glück

ResponseFormat = WebMessageFormat.Json, 
    RequestFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Wrapped, 
+0

Haben Sie das gesehen - http://Stackoverflow.com/a/17534969/903324? –

+0

keine Auswirkungen hatte die folgende Zugabe

+0

[WebInvoke (Methode = "POST", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "auth")] –

Antwort

0

Wenn Sie Host den Dienst sicherstellen, dass Ihr svc sieht aus wie

<%@ ServiceHost Language="C#" Debug="true" Service="Service" Factory="System.ServiceModel.Activation.WebServiceHostFactory" 
die folgenden Argumente für diese Methode auf
+0

Vielen Dank! Das funktioniert perfekt. –