2016-05-31 55 views
0

我创建了一个小的MVC项目,我想通过我的JavaScript控制器检索数据AngularJS与MVC ERR_CONNECTION_REFUSED

我的控制器看起来是这样的: -

angular.module('MyApp', []) 
    .controller('PlayerCtrl', function ($scope, $http) { 
    $scope.title = "loading..."; 
    $scope.players = []; 
    $scope.working = false; 
    $scope.myString = "ABC"; 

    $scope.getPlayers = function (group) { 
     $scope.working = true; 

     $http.get("/api/player").success(function (data, status, headers, config) { 
      $scope.players = data.players; 

     }).error(function (data, status, headers, config) { 
      $scope.title = "Oops... something went wrong"; 
      $scope.working = false; 
     }); 
    }; 

}); 

我的HTML页面看起来像这样: -

@{ 
    ViewBag.Title = "Play"; 
} 

<div id="bodyContainer" ng-app="MyApp"> 
    <section id="content"> 
    <div ng-model="PlayerCtrl" ng-init="getPlayers()"> 
     <p>{{myString}}</p> 
     <select id="group1"> 
      <option ng-repeat="option in data.players" value="{{option.FirstName}}">{{option.LastName}}</option> 
     </select> 
    </div> 
    </section> 
</div> 

@section scripts { 
    @Scripts.Render("~/Scripts/angular.js") 
    @Scripts.Render("~/Scripts/app/player-controller.js") 
} 

我可以看到我的JavaScript被击中,因为我已经在Chrome开发人员工具断点

然而,当执行JavaScript的我在开发工具

http://localhost:61176/4181b55e60dd4855bf01360ebafcfd61/browserLink无法加载资源的控制台收到以下错误:净:: ERR_CONNECTION_REFUSED

我不知道为什么。任何指针极大赞赏

注 - 如果我添加/ api /播放器到我的URL测试然后我的.NET代码触发并正确执行。从javascript控制器正常运行时,我的.NET代码不会被调用

回答

1

您没有发布您的控制器代码,所以我不知道它是否相关,但请确保您的JSON操作设置为JsonRequestBehavior.AllowGet
例如:

public JsonResult GetAllCustomers() 
{ 
    var customers = new List<Customer> 
    { 
     new Customer { Id = 1, Name = "a"}, 
     new Customer { Id = 2, Name = "b"}, 
     new Customer { Id = 3, Name = "c"}, 
     new Customer { Id = 4, Name = "d"} 
    }; 
    return Json(customers, JsonRequestBehavior.AllowGet); // here 
} 

,你应该在你是js代码位于不同的域比你controller是,你必须启用cors检查的情况下另一件事。

0

我在我的div上错误地使用了ng-model而不是ng-controller。