簡體   English   中英

從外部域調用 WCF 服務

[英]Calling WCF service from external domain

我正在嘗試從外部域調用 WCF 服務。 似乎我在 Fiddler 中得到了正確的答案,但是 $ajax 調用返回一個錯誤:

錯誤:未調用 MyCallback

示例應用:

<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>

編輯:WCF 服務合同:

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

該問題與錯誤的 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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM