不从ASP.Net调用WCF服务方法

时间:2015-04-30 04:39:35

标签: asp.net ajax wcf

我试图使用AJAX从ASP.NET调用页面加载时调用WCF方法。但是,即使执行代码,当我的页面加载时也没有任何反应。我也没有任何错误。以下是我写的代码片段。我在这里错过了什么吗?

AJAX代码 - 该服务在IIS中正确配置,.svc正在浏览,没有任何错误。

<script lang="javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "http://localhost/FinService.svc/GetMessage",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var value = JSON.stringify(response);
                alert(value);
            },
            failure: function (response) {
                alert(response.d);
            }
        });
    });
</script>

WCF方法 - 接口

namespace Fin.ServiceContracts
{
  [ServiceContract]
  public partial interface IFinService
  {
    [OperationContract(Name = "GetMessage")]
    [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)]
    string GetMessage();
  }
}

WCF方法实现

namespace Fin.Service
{
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public partial class FinService : IFinService
  {
    public string GetMessage()
    {
        return "hi from wcf";
    }
  }
}

的Web.config

<system.serviceModel>
  <bindings>
      <webHttpBinding>
        <binding name="WebBinding"/>
      </webHttpBinding>
  </bindings>

  <services>
     <service behaviorConfiguration="sbehaviour" name="myservice">
        <endpoint address="" binding ="WebBinding" contract ="Fin.ServiceContracts.IFinService" behaviorConfiguration="WebHttpBidning_IPublicService"/>
     </service>
  </services>

  <behaviors>
    <endpointBehaviors>
        <behavior name="WebHttpBidning_IPublicService">
            <webHttp/>
        </behavior>
    </endpointBehaviors>

    <serviceBehaviors>
        <behavior name="sbehaviour">
           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
           <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
   </behaviors>
  </system.serviceModel>

0 个答案:

没有答案