Erez Konforti
Erez Konforti

Reputation: 253

$http get fails

In my controller I've built GetUsersByJson Action:

public ActionResult GetUsersByJson()
    {
        DataLayer dal = new DataLayer();
        return Json(dal.Users.ToList<User>(), JsonRequestBehavior.AllowGet);
    }

dal.Users.ToList<User>() retrieves users from my database, this method works well and the retrieved JSON is valid.

Then in a script I did:

$scope.load = function () {
                $http({ method: "GET", url: "GetUsersByJson" }).
                success(function (data, status, headers, config) {
                    $scope.users = data;
                });
            };
$scope.load();

but it doesn't go into the success function.

Upvotes: 0

Views: 70

Answers (3)

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

You had wrong URL in your $http.get call it should be /MyController/GetUsersByJson as default route is been already there in RouteConfig.cs.

Also you need to use .then instead of .success/.error method, since they are $http callbacks are deprecated.

Code

$scope.load = function () {
    $http({ method: "GET", url: "/MyController/GetUsersByJson" })
    .then(function (response) {
         $scope.users = response.data;
     }, function(error){
         console.log(error); //this will print error in console.
     });
};
$scope.load();

Upvotes: 1

D.Jacobi
D.Jacobi

Reputation: 29

instead of

success(function (data, status, headers, config) {
                $scope.users = data;
            });

Try

.then(function (data) {
                $scope.users = data;
            });

Upvotes: 0

tarun_
tarun_

Reputation: 9

Try using .then instead of .success See this.

Upvotes: 0

Related Questions