2012-07-13 13 views
8

Estoy intentando llamar a un servicio web WCF desde una página ASPX de este modo:

var payload = { 
    applicationKey: 40868578 
}; 

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

Si lo hace, los resultados en el servidor web devolver el error 415 Unsupported Media Type. Estoy seguro de que este es un problema de configuración con el servicio WCF que se define de la siguiente manera:

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

No hay entradas en el archivo web.config por lo asuma que el servicio utiliza la configuración por defecto.

Respuesta

4

No soy un experto en esto, de hecho tuve el mismo problema (por otra razón). Sin embargo, parece que los servicios de WCF no son inherentemente compatibles con AJAX y, por lo tanto, debe tener el siguiente código en su archivo web.config para habilitarlo.

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

y luego esto en la clase de servicio

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

Esto es lo que se ha generado por VS 2012 cuando he añadido un AJAX servicio WCF.

Cuestiones relacionadas