【问题标题】:.NET controller does not get hit - GET method.NET 控制器未命中 - GET 方法
【发布时间】:2019-12-06 18:05:37
【问题描述】:

我正在尝试使用 Angular ng-repeat 将 JSON 请求的结果放入表中,但没有任何反应。控制台未显示错误,.NET 控制器中的断点未命中。

应用控制器代码:

var app = angular.module('angularTable', []);

app.controller('listdata', function ($scope, $http) {

    $scope.contacts = []; 
    $http({ method: 'GET', url: '/Contacts/GetContacts/' }).success(function (response) {
        $scope.contacts = response; 
    });

});

.NET 控制器

[HttpGet]
    public JsonResult GetContacts()
    {
        var data = db.Contacts.Include(x => x.Cities).Include(a => a.Groups);

        return Json(data);
    }

HTML:

<div ng-app ="angularTable">
 <table class="table table-hover" ng-controller="listdata">
        <thead>
            <tr>

                <th>Name</th>
                <th>Phone</a></th>
                <th>Group</th>
                <th>City</th>
                <th>Actions</th>
            </tr>

        </thead>
        <tbody>
            <tr ng-repeat="item in contacts">
                <td>{{item.name}}</td>
                <td>{{item.area}} {{item.phone}}</td>
                <td>{{item.group.description}}</td>
                <td>{{item.city.name}}</td>
                <td>
                    <a asp-route-id="{{item.id_Contact}}">Send Message | </a>
                    <a asp-route-id="{{item.id_Contact}}">Edit | </a>
                    <a asp-route-id="{{item.id_Contact}}" asp-action="Delete">Delete</a>
                </td>
            </tr>

        </tbody>
    </table>
  </div>

将此 CDN 用于 AngularJS:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

【问题讨论】:

标签: javascript c# angularjs asp.net-core


【解决方案1】:

首先,正如@ThiagoCustodio 所说,请在浏览器中输入url,检查是否可以获得预期的json结果。

其次,您可以尝试修改如下代码以允许来自客户端的 HTTP GET 请求。

public JsonResult GetContacts()
{
    var data = db.Contacts.Include(x => x.Cities).Include(a => a.Groups);


    return Json(data, JsonRequestBehavior.AllowGet);
}

此外,如果您的 AngularJS 客户端作为单独的应用程序运行,请检查您是否在后端(服务)应用程序中为特定来源启用/配置 CORS。

【讨论】:

  • 我最终解决了,问题是我的 Angular 表位于通过 AJAX 加载的局部视图中,由于某种原因 AngularJS 在局部视图中不起作用。
猜你喜欢
  • 2021-08-15
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多