Consuming a WCF Service in jQuery via AJAX Call in a different Project (Cross Domain) Consuming a WCF Service in jQuery via AJAX Call in a different Project (Cross Domain) json json

Consuming a WCF Service in jQuery via AJAX Call in a different Project (Cross Domain)


I could not find any solution for a cross domain WCF jQuery AJAX Call. Therefore, I'm posting here that how I have solved this issue.

There is no need to give data when I use GET method in AJAX Call.

The things that you must consider when consuming WCF in jQuery AJAX call (cross domain);

  1. Run the WCF Service Project in separate instance of Visual Studio. Do not mix WCF Service Project and Consuming Project in one instance and run at once. When you run the consuming project, the WCF Project must already be up and running.
  2. Use DataType as jsonp instead of json.
  3. In web.config of WCF Project, make sure you have the attribute crossDomainScriptAccessEnabled="true" in <binding> tag under <system.serviceModel>\<bindings>\<webHttpBinding>. Also the binding name set to bindingConfiguration attribute in the <endpoint> tag.

For futher information,Following is my jQuery AJAX Call;

$.ajax({   cache: false,   type: "GET",   async: false,   processData: true,   data: "",   url: "http://localhost:64973/Account.svc/GetAccountByID/2000",   contentType: "application/json",   dataType: "jsonp",   success: function (result) {       alert(result);       alert(result.GetAccountByIDResult);   }});

Following is my web.config;

<?xml version="1.0"?><configuration>  <appSettings>    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  </appSettings>  <system.web>    <compilation debug="true" targetFramework="4.5" />    <httpRuntime targetFramework="4.5"/>  </system.web>  <system.serviceModel>    <bindings>      <webHttpBinding>        <binding name="crossDomain" crossDomainScriptAccessEnabled="true" />      </webHttpBinding>    </bindings>    <behaviors>      <endpointBehaviors>        <behavior name="tSeyvaWCFEndPointBehavior">          <webHttp />        </behavior>      </endpointBehaviors>      <serviceBehaviors>        <behavior name="tSeyvaServiceBehavior">          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />          <serviceDebug includeExceptionDetailInFaults="true" />        </behavior>      </serviceBehaviors>    </behaviors>    <protocolMapping>      <add binding="basicHttpsBinding" scheme="https" />    </protocolMapping>    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />    <services>      <service name="tSeyva.WCF.Account" behaviorConfiguration="tSeyvaServiceBehavior">        <endpoint address=""                  behaviorConfiguration="tSeyvaWCFEndPointBehavior"                  bindingConfiguration="crossDomain"                  binding="webHttpBinding"                  contract="tSeyva.WCF.IAccount">        </endpoint>      </service>    </services>  </system.serviceModel>  <system.webServer>    <modules runAllManagedModulesForAllRequests="true"/>    <!--        To browse web app root directory during debugging, set the value below to true.        Set to false before deployment to avoid disclosing web app folder information.      -->    <directoryBrowse enabled="true"/>  </system.webServer></configuration>