jueves, 12 de septiembre de 2013

javascript - Llamar a un Servicio Web WCF desde javascript (cogido de otro foro)


Suppose this is our simple WCF service which takes the name as input and returns the resulting string with Hello appended to it.

namespace HelloWorldWCFService{
[ServiceContract]
public interface IService1{
[OperationContract]
string GetData(string name);}
}

namespace HelloWorldWCFService{
public class Service1 : IService1 {
public string GetData(string name) {
return "Hello " + name;
}}}

It uses BasicHttpBinding.
Now this would be our JavaScript function which can consume the above WCF service.

function MyRequest() {var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://xx:64833/xx.svc", false);
xmlhttp.setRequestHeader("SOAPAction", "http://pep.org/IService1/GetData");

xmlhttp.setRequestHeader("Content-Type", "text/xml");
var data = ';
data += "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>";data += " <s:Body>";

data += " <GetData xmlns='http://tempuri.org/'>";

data += " <name>Nishant</name>";

data += " </GetData>";

data += " </s:Body>";
data += "</s:Envelope>";xmlhttp.send(data);
alert("status = " + xmlhttp.status);alert("status text = " + xmlhttp.statusText);

alert(xmlhttp.responseText);
}

Here for
xmlhttp.setRequestHeader("SOAPAction", "http://pep.org/IService1/GetData");

We can get the value for SOAPAction from the wsdl of the WCF service.
Open the http://xx:64833/xx.svc?wsdl
Search for soapAction there.
We would find soapAction value there.

<wsdl:operation name='GetData'>  <soap:operation
soapAction='http://tempuri.org/IService1/GetData' style='document' />
<wsdl:input>

 

No hay comentarios:

Publicar un comentario