Tengo un método OperationContract
en el que trato de consultar e insertar datos en la base de datos. Estoy utilizando un método POST
y llamando al servicio desde javascript en el navegador. El Servicio WCF está en el mismo dominio, por lo que no debería tener que usar JSONP
. También estoy modificando los datos por lo que debe ser una solicitud POST
no una solicitud GET
. Sin embargo, sigo obteniendo un "Error de método no permitido". ¿Alguien ha encontrado esta situación?WCF WebInvoke POST - Método no permitido error
Mi servicio está siendo llamado a
http://some_url.com/services/ProfileService.svc/json/CurrentUser
Curiosamente, parece ser llamado a través de una solicitud GET
cuando voy esta url a pesar de que specifiy un POST
. En la carga de la página, sin embargo, parece estar intentando una solicitud POST
.
respuesta navegador cuando va a la 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
Aquí es mi método que estoy tratando de llamar:
[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);
}
});
}
No estoy seguro de si es necesario pero 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>
Editar para Web.Config - Adición de enlaces de
<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>
Esto no resolverá su problema pero es una buena práctica pasar '' {} '' como datos en lugar de nulo (http://encosia.com/3-mistakes-to-avoid-when-using-jquery -with-aspnet-ajax /) – Jupaol
¡Gracias por el artículo! – jocey
No puedo encontrar nada incorrecto con su código, acabo de crear un servicio similar con la misma configuración y funciona correctamente, la única diferencia es 'bindingConfiguration =" secure "', supongo que no publicó esa vinculación por razones de seguridad, bien si pudieras publicarlo sería útil. ¿Has intentado eliminar esa configuración de enlace y ver si puedes acceder a tu servicio? – Jupaol