user601367
user601367

Reputation: 2368

Consuming wcf service in jquery

I am facing problem in consuming wcf service.The responce in ajax is going to error function. The url is perfectly right.

    $.ajax({
            type: "post",
            url: "http://192.168.1.3:16150/ajaxnewclinicalhost/Service.svc/gettablejson1",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert(msg);
            },
            error: function () {
                alert("error");
            }
           });

Web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <compilation debug="false" targetFramework="4.0">
            <assemblies>
                <add assembly="System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /></assemblies></compilation>
    </system.web>
    <system.serviceModel>
        <services>
   <service name="newclinicallibrary.Service1">
    <endpoint address="" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="WEB" contract="newclinicallibrary.IService1" />
   </service>
  </services>
  <behaviors>
            <serviceBehaviors>
                <behavior>
                    <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                    <serviceMetadata httpGetEnabled="true" />
                    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="WEB">
          <webHttp />
        </behavior>
      </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <directoryBrowse enabled="true" />
    </system.webServer>
</configuration>

Upvotes: 1

Views: 667

Answers (1)

Andy Holt
Andy Holt

Reputation: 572

Whenever making AJAX calls, you should bear in mind that for security reasons the browser will only allow requests to be made to URLs on the same domain as the page itself. So unless your page is being served up from http://192.168.1.3, you wont be able to make AJAX calls to your WCF service.

If this does turn out to be the case, you could try one of the following:

  • Host your WCF on a sub-domain of your website (might still suffer from security issues due to the differing port).
  • Write an intermediate script in your website which brokers requests to your WCF service.
  • Don't use WCF - instead house your functionality in a script in your website. If you are using ASP.NET, then an ASHX or web module would be suitable.

Upvotes: 1

Related Questions