Pvxtotal
Pvxtotal

Reputation: 89

.NET controller does not get hit - GET method

I'm trying to get the result of a JSON request into a table using angular ng-repeat, but nothing happens. Console shows no errors, breakpoint in .NET Controller doesn't get hit.

Code for App Controller:

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 Controller

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

Using this CDN for AngularJS:

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

Upvotes: 0

Views: 148

Answers (1)

Fei Han
Fei Han

Reputation: 27793

Firstly, as @ThiagoCustodio mentioned, please enter the url in your browser and check if you can get expected json result.

Secondly, you can try to modify the code like below to allow HTTP GET requests from the client.

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


    return Json(data, JsonRequestBehavior.AllowGet);
}

Besides, if your AngularJS client runs as a separate application, please check if you enable/configure CORS for specific origins in your back-end (service) application.

Upvotes: 1

Related Questions