F.N.B
F.N.B

Reputation: 1619

Angular REST client ngResource can't get json

I have simple RESTful server with Flask and I like to make a simple client with AngularJS using ngResource. The idea is make a GET to the server, and obtain a json file.

This is my services.js

var IpZapServices = angular.module('IpZapServices', ['ngResource']);
IpZapServices.factory('Plug', ['$resource', function($resource) {
    return $resource('http://localhost:8003/api/plugs/:id',
             {id : "@id"}, {
                 query: {method: 'GET', params: {}, isArray: false}
           });
}]);

And the controllers.js

var IpZapControllers = angular.module('IpZapControllers', []);
IpZapControllers.controller('PlugListCtrl', ['$scope', 'Plug', function($scope, Plug) {
    $scope.plugs = Plug.query();
    console.log($scope.plugs);
}]);

But, I don't get the json file, get this:

Object { $promise: Object, $resolved: false }

Why? What's I do wrong? Thanks!

EDIT:

This is the raw response that I receipt from the server.

{"plugs": [
    {"alarm": [],
     "id": 0,
     "name": "Plug 0",
     "state": false},
    .
    .
    .
    {"alarm": [],
     "id": 3,
     "name": "Plug 3",
     "state": false}
]}

EDIT 2: Solution

The problem is in the server. Simply add to the server Flask-CORS and work!

from flask.ext.cors import CORS
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

The solution is from this question

Upvotes: 0

Views: 371

Answers (4)

FlorianTopf
FlorianTopf

Reputation: 948

You need to resolve the promise you have created with Plug.query()

Try this:

Plug.query().$promise.then(function (response) {
     console.log(response)
     $scope.plugs = response;
});

More information on this can be found in the docs on angularjs.org:

https://docs.angularjs.org/api/ngResource/service/$resource

Upvotes: 2

Anditthas
Anditthas

Reputation: 541

It seems that you have a promise there.

Don't assign the return value of the function, you need to wait till the promise resolves or rejects

Try

Plug.query().$promise
    .then(function (response) {
         // success code here
     }) 
    .catch(function (err) {})

Upvotes: 1

jsonmurphy
jsonmurphy

Reputation: 1600

Try this:

var IpZapControllers = angular.module('IpZapControllers', []);
IpZapControllers.controller('PlugListCtrl', ['$scope', 'Plug', function($scope, Plug) {
  $scope.plugs = Plug.query();
  $scope.plugs.$promise.then(function (result) {
      console.log(result);
      $scope.plugs = result;
  });
}]);

Explanation:

Your call to query will not return the data immediately. It instead returns a promise object that will eventually get your data once the HTTP request is complete (notice that the console message said resolved: false).

Read More:

Promises in AngularJS, Explained as a Cartoon

Upvotes: 1

Tomaltach
Tomaltach

Reputation: 923

Whwn connecting to a webapi from the controller, use the success and error promise from the method call.

Ctrl.$inject = ["$scope", "Service"];
function Ctrl($scope, Service) {

    Service.getJson().success(function (data) {
        $scope.json = data;
    })
    .error(function (data) {
        // data should show the error
    });
}

Upvotes: 0

Related Questions