2017-01-26 12 views
1

外部ドメインからWCFサービスを呼び出そうとしています。私がバイオリンで答えが正しい取得していますが、$のAJAX呼び出しがエラーを返すようだ:外部ドメインからWCFサービスを呼び出す

Error: MyCallback was not called

サンプル・アプリケーション:

<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サービス契約:

[ServiceContract(SessionMode = SessionMode.NotAllowed)] 
public interface ILocationService 
{ 
    [OperationContract] 
    [WebGet(ResponseFormat = WebMessageFormat.Json)] 
    string GetLocation(int id); 
} 
+0

チェックアウト、この:http://stackoverflow.com/questions/23756709/calling-a-wcf-service-with-jquery-ajax –

+0

あなたの提供するリンクには、あなたが参照しているマーク付きの回答がありませんか? – Whistler

答えて

0

問題が間違っているWebサービスのweb.config:

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

と:

<services> 
    <service name="Services.LocationService" behaviorConfiguration="serviceBehavior"> 
    <endpoint address="" binding="webHttpBinding" 
       bindingConfiguration="webHttpBindingWithJsonP" 
       contract="Services.IkLocationService" behaviorConfiguration="webBehavior" /> 
    </service> 
</services> 
関連する問題