2012-07-13 11 views
8

Ja próbuje wywołać usługę WWW WCF od strony aspx tak:

var payload = { 
    applicationKey: 40868578 
}; 

$.ajax({ 
    url: "/Services/AjaxSupportService.svc/ReNotify", 
    type: "POST", 
    data: JSON.stringify(payload), 
    contentType: "application/json", 
    dataType: "json" 
}); 

Takie postępowanie prowadzi do serwera WWW powrocie błąd 415 Unsupported Media Type. Jestem pewien, że jest to problem z konfiguracją usługi WCF, która jest zdefiniowana w następujący sposób:

[OperationContract] 
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)] 
void ReNotify(int applicationKey); 

Brak wpisów w pliku web.config więc zakładamy, że serwis korzysta z domyślnej konfiguracji.

Odpowiedz

4

Nie jestem ekspertem w tym, w rzeczywistości miałem ten sam problem (z innego powodu). Wydaje się jednak, że usługi WCF z natury nie obsługują technologii AJAX, dlatego w pliku web.config musisz mieć następujący kod, aby go włączyć.

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="NAMESPACE.AjaxAspNetAjaxBehavior"> 
       <enableWebScript /> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
    <services> 
     <service name="NAMESPACE.SERVICECLASS"> 
      <endpoint address="" behaviorConfiguration="NAMESPACE.AjaxAspNetAjaxBehavior" 
       binding="webHttpBinding" contract="NAMESPACE.SERVICECLASS" /> 
     </service> 
    </services> 
</system.serviceModel> 

i wtedy to w klasie usług

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
using System.Text; 

namespace NAMESPACE 
{ 
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)] 
    [ServiceContract(Namespace = "")] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class SERVICECLASS 
    { 
     // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json) 
     // To create an operation that returns XML, 
     //  add [WebGet(ResponseFormat=WebMessageFormat.Xml)], 
     //  and include the following line in the operation body: 
     //   WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 
     [OperationContract] 
     public string DoWork() 
     { 
      // Add your operation implementation here 
      return "Success"; 
     } 

     // Add more operations here and mark them with [OperationContract] 
    } 
} 

to co zostało wygenerowane przez VS 2012, kiedy Dodano AJAX włączone usługi WCF.

Powiązane problemy