Friday, February 8, 2013

Call custom WCF Soap Service from Javascript

In my posts regarding oData, you can find JavaScript calls against WCF DataServices. Here i describe the call against a default WCF SOAP Endpoint.

WCF Service:
  public class Service1 : IService1 {
      public string GetData(int value) {
          return string.Format("You entered: {0}", value);
      }
  }

JavaScript:
function Retrieve(id) {
 jQuery.support.cors = true;
 var soapData = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' + '<s:Body><GetData xmlns="http://tempuri.org/"><value>200</value></GetData></s:Body></s:Envelope>';
 $.ajax({
     type: "POST",
     contentType: "text/xml; charset=utf-8",
     dataType: "xml",
     url: "http://localhost:58282/Service1.svc",
     data: soapData,
     beforeSend: function (xhr) {
       xhr.setRequestHeader("SOAPAction""http://tempuri.org/IService1/GetData");
     },
     success: RetrieveCallbackSuccess,
     error: RetrieveCallbackError
 });
}
function RetrieveCallbackSuccess(response) {     alert("Yey! :) --> " + response.text); }
function RetrieveCallbackError(err) {     alert("Onoz! :( --> " + err.statusText); }

The line "jQuery.support.cors = true" is important for cross domain requests.

Cheers,
Markus

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete