2

I'm trying to call WCF service from external domain. It seems that I'm getting answer correct in Fiddler, but $ajax call return an error:

Error: MyCallback was not called

Sample application:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#button1").click(function(){
            $.ajax({
                url: "http://localhost:31492/LocationService.svc/GetLocation",
                data: '{"id":"33"}',
                dataType: "jsonp",
                type: "GET",
                timeout: 10000,
                jsonpCallback: "MyCallback",
                success: function (data, textStatus, jqXHR) {
                    $("#display").html(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {    
                    alert(errorThrown);
                },
                complete: function (jqXHR, textStatus) {
                }
            });
            function MyCallback(data) {
            alert(data);
        }
        });
    });
    </script>
</head>

<body>
    <h1>Test</h1>
    <br><br><br>
    <div id="display">
    </div><br>
    <button id="button1">Get External Content</button>
</body>

EDIT: WCF Service Contract:

[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public interface ILocationService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    string GetLocation(int id);
}
Whistler
  • 1,897
  • 4
  • 29
  • 50

1 Answers1

1

The issue was related to incorrectly web service web.config:

<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
  </webHttpBinding>
</bindings>

and:

<services>
  <service name="Services.LocationService" behaviorConfiguration="serviceBehavior">
    <endpoint address="" binding="webHttpBinding"
              bindingConfiguration="webHttpBindingWithJsonP"
              contract="Services.IkLocationService" behaviorConfiguration="webBehavior" />
  </service>
</services> 
Whistler
  • 1,897
  • 4
  • 29
  • 50