从angularjs控制器调用WCF服务时出错

时间:2017-08-01 05:29:02

标签: javascript c# angularjs ajax wcf

我是angularjs的新手。在学习的过程中,我创建了一个测试应用程序,在其中我创建了一个启用了ajax的WCF服务和一个带有angularjs代码的测试页面。 使用$ http.get()时,控件无法访问服务方法并显示错误。

我的angularjs测试页面如下

<body>
<form id="form1" runat="server">
<div>
        <div ng-app="myAngularJs" ng-controller="myCtrl">
        </div>
</div>
</form>
</body>

只是尝试访问该服务并没有做任何事情。 控制器在js文件中,如下所示

angular.module("myAngularJs", []).controller("myCtrl", function ($scope, $http) {
    $http({
        url: "http://localhost:17040/Service1.svc/getData",
        method: "GET"
    }).then(function (data) {
        debugger;
        console.log(data);
        var parsed = JSON.parse(data.data.d);
    });
});

我通过添加新的启用了ajax的WCF服务在同一个项目中创建了WCF服务。其代码如下

[OperationContract]        
    [WebGet(UriTemplate = "/getData/", ResponseFormat = WebMessageFormat.Json)]
    public string getData()
    {
        ServiceReference.WcfServiceClient obj = new ServiceReference.WcfServiceClient();
        string data = obj.getTestAjaxget();
        return data;
    }

我的问题是控件永远不会到达服务但是错误会在浏览器控制台中显示如下 无法加载资源:服务器响应状态为500(内部服务器错误)

我的web.config如下

<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>

<system.serviceModel>
  <bindings>
    <webHttpBinding>
      <binding crossDomainScriptAccessEnabled="true" name=""></binding>
    </webHttpBinding>
  </bindings>
    <behaviors>
        <endpointBehaviors>
          <behavior name="ServiceAPI.WcfServiceAspNetAjaxBehavior">
            <enableWebScript />
          </behavior>
            <behavior name="testApplication.Service1AspNetAjaxBehavior">
                <enableWebScript />
            </behavior>              
        </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
        multipleSiteBindingsEnabled="true" />
    <services>
        <service name="testApplication.Service1">
            <endpoint address="" behaviorConfiguration="testApplication.Service1AspNetAjaxBehavior"
                binding="webHttpBinding" contract="testApplication.Service1" />
          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
  <client>
    <endpoint address="http://localhost:6168/WcfService.svc" behaviorConfiguration="ServiceAPI.WcfServiceAspNetAjaxBehavior"
     binding="webHttpBinding" contract="ServiceReference.WcfService" />
  </client>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

0 个答案:

没有答案