I have a WebService
to which I want to access its methods, I have searched but I have not found a solution on how to attack the service, I .svc
only find services .asmx
.
From c# I add the service reference and to use the method I do the following:
ServicioClient SClient = new ServicioClient();
Gestores_SesionResponse sesionRespuesta = null;
Gestores_SesionRequest datosSesion = new Gestores_SesionRequest(){
Pwd = model.Password,
UserName = model.UserName
};
SClient.dbGestores_Sesion(datosSesion, out sesionRespuesta);
But I want to attack the same service from jquery , I have tried as follows:
$.ajax({
url: 'http://nombredominio.com.mx:1234/Service.svc/dbGestores_Sesion',
data: '{"UserName":"usuario","Pwd":"123456"}',
contentType: 'application/json; charset=utf-8',
method: 'post',
dataType: 'json',
processdata: true,
success: function (data) {
console.log("exito");
console.log(data);
},
error: function (result) {
console.log("error");
console.log(result);
}
});
But when making the request it sends meerror 400
XMLHttpRequest cannot load http://domainname.com.mc:1234/service.svc/dbGestores_Session . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:50478 ' is therefore not allowed access. The response had HTTP status code 400.
Where dbGestores_Session
is the name of the method, and receives 2 username and password parameters, so how can I access my method WebService.svc
from jquery ? And how can I get the data from the out
one I use in c# with jquery ?
Note: If I access http://domainname.com.mc:1234/service.svc it shows me the service page but if I access this way http://domainname.com.mc:1234/service.svc/dbGestores_Session only show a blank page
In your
Web.config
give it the following configuration:In case you get the following error:
and the origin is the line where it is located:
Go to the Web Platform Installer, find and install " URL Rewrite ", it is the IIS module that is needed.