2012-07-10 11 views
6

Ho un metodo OperationContract in cui sto cercando di eseguire query e inserire dati nel database. Sto usando un metodo POST e chiama il servizio da javascript nel browser. Il servizio WCF si trova nello stesso dominio, quindi non dovrei usare JSONP. Sto anche modificando i dati in modo che dovrebbe essere una richiesta POST non una richiesta GET. Tuttavia sto ancora ricevendo un "Errore metodo non consentito". Qualcuno ha incontrato questa situazione?POST WebInvoke WCF - Errore metodo non consentito

Il mio servizio è chiamato a

http://some_url.com/services/ProfileService.svc/json/CurrentUser 

Stranamente, sembra di essere chiamato tramite una richiesta GET quando vado questo URL, anche se ho un specifiy POST. Sul caricamento della pagina, tuttavia, sembra che si stia tentando una richiesta POST.

risposta del browser quando si va al url:

Request URL:http://some_url.com/services/ProfileService.svc/json/CurrentUser 
Request Method:GET 
Status Code:405 Method Not Allowed 
Request Headersview parsed 
GET /services/ProfileService.svc/json/CurrentUser HTTP/1.1 

Ecco il mio metodo che sto provando a chiamare:

[OperationContract] 
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)] 

public HIPUser GetCurrentUser() 
{ 
    string[] domainUser; 
    string Auth_User = HttpContext.Current.User.Identity.Name.ToString().ToLower(); 
    domainUser = Auth_User.Split('\\'); 
    string user = domainUser[1]; 
    Debug.WriteLine(user); 

    ProfileManager pm = new ProfileManager(); 
    var results = pm.GetUserByUserName(user); 

    if (results.Length > 0) 
    { 
     return results.First(); 
    } 
    else 
    { 
     Debug.WriteLine("IS NULL"); 
     var x = pm.CreateUser(user, null, null); 
     Debug.WriteLine(x.UserName); 
     return x; 
    } 
} 

Cliente:

function getCurrentUser() { 

$.ajax({ 
    type: "POST", 
    url: "services/ProfileService.svc/json/GetCurrentUser", 
    contentType: "application/json; charset=utf-8", 
    data: null, 
    dataType: "json", 
    error: function (request, error, u) { 
     alert('blargherror: ' + error); 
    }, 
    success: function (result, status) { 
     alert(result.d); 
    } 
}); 
} 

Non sono sicuro se necessario ma Web.Config:

<behaviors> 
    <endpointBehaviors> 
     <behavior name="jsonBehavior"> 
      <enableWebScript /> 
     </behavior> 
    </endpointBehaviors> 

    <serviceBehaviors> 
     <behavior name="metaBehavior"> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
      <serviceMetadata httpGetEnabled="true" /> 
     </behavior> 
    </serviceBehaviors> 
</behaviors> 

<serviceHostingEnvironment 
    aspNetCompatibilityEnabled="true" 
    multipleSiteBindingsEnabled="true" /> 

<services> 
    <service name="ProfileService" behaviorConfiguration="metaBehavior"> 
     <endpoint address="/json" 
      behaviorConfiguration="jsonBehavior" 
      binding="webHttpBinding" 
      bindingConfiguration="secure" 
      contract="ProfileService" /> 
     <endpoint address="" 
      binding="basicHttpBinding" 
      bindingConfiguration="secure" 
      contract="ProfileService" /> 
    </service> 
</services> 

Modifica per Web.Config - Aggiunta di attacchi

<bindings> 
     <webHttpBinding> 
      <binding name="secure"> 
       <security mode="TransportCredentialOnly"> 
        <transport clientCredentialType="Windows"/> 
       </security> 
      </binding> 
     </webHttpBinding> 
     <basicHttpBinding> 
      <binding name="secure"> 
       <security mode="TransportCredentialOnly"> 
        <transport clientCredentialType="Windows" /> 
       </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
+2

Questo non risolverà il tuo problema ma è una buona pratica passare "" {} "' come dato invece di null (http://encosia.com/3-mistakes-to-avoid-when-using-jquery -with-aspnet-ajax /) – Jupaol

+0

Grazie per l'articolo! – jocey

+2

Non riesco a trovare nulla di sbagliato con il tuo codice, ho appena creato un servizio simile con la stessa configurazione e funziona correttamente, l'unica differenza è 'bindingConfiguration =" secure "', immagino che non hai postato quell'associazione per motivi di sicurezza, beh se potessi postarlo sarebbe utile. Hai provato a rimuovere quella configurazione vincolante e vedere se riesci ad accedere al tuo servizio ?? – Jupaol

risposta

1

sono stato in grado di capire questo fuori. Si è verificato un problema con l'oggetto restituito di tipo sconosciuto, pertanto non è stato possibile serializzarlo in un oggetto JSON. Non ero sicuro di come serializzare l'oggetto per farlo funzionare con il mio metodo, così ho finito solo cambiando il metodo per restituire una stringa costruendo la mia stringa personalizzata JSON e usando la funzione eval() per trasformarla in un oggetto JSON.

Problemi correlati