locked
WCF with Jquery RRS feed

  • Question

  • User901332479 posted

    Dear Experts,

    My architecture is mvc2 in my view page i am calling following jquery ajax to call wcf service,  but it  is not calling  either success or failure , help me to get atleast failure part

     $.ajax({
                type: "POST",
                // url: "http://localhost:58748/TestWeb_service1.asmx/Test",
                url: "http://localhost:63263/Service1.svc/GetRefDoctorlist",
              
                data: '{"centerid": "' + 4 + '","RefName": "' + "a" + '"}',
                contentType: "application/json; charset=utf-8",
                processdata: true,
                dataType: "json",
                success: function (data) {
                    debugger;
                    if (data.d != null) {

                        var s = data.d[0];
                    }
                    else {

                    }

                },
                failure: function (msg) {
                    debugger;
                    alert('failure');

                }
            });

     

    The service is working fine in testing link

    the folowing is my web.config

    <system.serviceModel>
        <services>
          <service name="WCFranchise.Service1">
            <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="MEDALL.IDAL.IAppointment"/>
          </service>
        </services>

        <client />
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata/>
              <serviceDebug includeExceptionDetailInFaults="false" />
              <dataContractSerializer maxItemsInObjectGraph="2147483647" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="webScriptEnablingBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <bindings>
          <wsHttpBinding >
            <binding name="NoSecurity"  >
              <security mode="None" />
            </binding>
          </wsHttpBinding>
        </bindings>

        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <defaultDocument>
          <files>
            <add value="Service1.svc" />
          </files>
        </defaultDocument>
      </system.webServer>

    Wednesday, February 26, 2014 12:39 AM

Answers

  • User-417640953 posted

    Hi 1991harihara,

    Thank you post the issue to asp.net forum.

    Based on your description and code, I see you want to call the cross domain wcf service using jquery ajax.

    First, a wcf service should fit below conditions if it can be called by ajax.

        #The Service has to be decorated with WebInvoke/WebGet to be accessed from javascript.

        #<enableWebScript/> has to be added to the configuration for enabling script calls to WCF.

        #webHttpBinding is to be used for the WCF to behave as a REST service.

    When you want to perform "POST" or "GET" request on cross domain wcf service or normal service using jquery/javascript or ajax,

    the browser actually sends an "OPTIONS" verb call to your wcf service that is not mention in your wcf method attribute.

    So we should change these options we need to do some changes in web.config of hosted wcf service.

    <system.webServer>
       <modules runAllManagedModulesForAllRequests="true" />
       <httpProtocol>
       <customHeaders>
           <add name="Access-Control-Allow-Origin" value="*" />
           <add name="Access-Control-Allow-Headers" value="Content-Type" /> 
       </customHeaders>
       </httpProtocol> 
    </system.webServer>
       <system.serviceModel>
        <behaviors>..............</behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
        <standardEndpoints>
        <webScriptEndpoint>
        <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
        </webScriptEndpoint>
        </standardEndpoints>
    

    For more information, you can follow below blog.

    http://www.dotnet-tricks.com/Tutorial/wcf/X8QN260412-Calling-Cross-Domain-WCF-Service-using-Jquery.html

    Hope this helps, thanks.

    Best Regards!

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 27, 2014 3:11 AM