2

I want to call my WCF service in my Mvc Application with this code:

 function SaveRouteCreation()
    {
        var serviceURL = 'http://localhost:36544/Service1.svc/GetData';
        $.ajax({
            type: "POST",
            url: serviceURL,
            data: param = "",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: successFunc,
            error: errorFunc
        });
}

Now this is my WCF Service project 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>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </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>

Now how do I call my WCF service in my Mvc Project?

Step 1: Add Service Reference of Wcf Project in my Mvc Application:

  • List item

  • click on Advanced option from Dialog box 1

  • 2

  • Now click on Add Web Reference

  • Add Web Reference dialog box:

enter image description here

Step 2: Is it possible without adding Service reference and directly calling my wcf service in mvc project with the help of Web.config settings (sorry, I don't know about this, just guessing).

When I am not adding service reference and calling my wcf service on button click event with AJAX I am getting this error in Firefox:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:36544/Service1.svc/GetData. (Reason: CORS request failed).

I have searched the internet but couldn't find anything useful and even tried some solution in case of cross origin like but it didn't work.

halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216

0 Answers0