python - AngularJS GET request to Flask/rest API -


i need if possible. building internal web application , have decided use angularjs. has been going fine, trying integrate/test calls flask rest api. following data, , mocked in api.

machine_data = {     "machine1": {         "machine_name": 'lx1host',         "machine_model": '12345678',         "machine_serial": '87654321'     } } 

i trying pass in variable view return above structure. here api method.

@app.route(v1_root_machine, methods=['get']) def machine_list():      try:         if 'hostname' in request.args:            target = request.args['hostname']     except:         target = none      machines = machine_data      if not target:        return general_error(error="no machines...")      message = {"status": 200,            "message": machines            }      resp = jsonify(message)     return resp 

i see call come in , 200 on flask side, see error on js console , states. i've been googling looks several things.

 consoleagent: error: [$resource:badcfg] http://errors.angularjs.org/1.3.15/$resource/badcfg?p0=query&p1=array&p2=object     @ error (native)     @ https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:6:417     @ q.then.p.$resolved (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-resource.min.js:9:330)     @ https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:112:113     @ n.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:126:15)     @ n.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:123:106)     @ n.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:126:293)     @ l (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:81:240)     @ m (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:85:342)     @ xmlhttprequest.f.onload (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:86:367) (url: https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js) in :327 

here snippet of view.

<tr ng-repeat="machine in data_table.message | filter:searchtext">  <td><a ui-sref="machineroute({hostname: machine.hostname">machine.hostname </a></td> 

my angular module , config.

var app = angular.module("app", ['ui.router','ngresource','testservices']);  app.config(function ($stateprovider, $urlrouterprovider) { $stateprovider     .state('machineroute', {             url: '/machineroute/:hostname',             templateurl: './machines.html',             controller: 'machinescontroller'         }) }); 

controller:

app.controller('machinescontroller', function ($scope, machineservice) {     $scope.data_table = machineservice.query(); }); 

service.js

    var boxservices = angular.module('boxservices', ['ngresource']);      boxservices.factory('machineservice', ['$resource',       function($resource){         return   $resource('http://127.0.0.1:5000/v1.0/machineroute/:hostname',                  {hostname: "@hostname"}, {  query: {method:'get', params: {}, isarray:false}     });   }]); 

i can make call httpie successful i'm pretty sure it's angular setup, can't figure out how pass variable correctly.

http http://127.0.0.1:5000/v1.0/machineroute/ hostname=testhost 

this return packet httpie call.

http/1.0 200 ok access-control-allow-headers: cont access-control-allow-methods: get, access-control-allow-origin: * content-length: 1685 content-type: application/json date: mon, 11 may 2015 22:50:41 gm server: werkzeug/0.10.4 python/2.7  {     "message": {        "machine1": {            "machine_name": 'lx1host',            "machine_model": '12345678',            "machine_serial": '87654321'         }     },     "status": 200 } 

thanks in advance..

so believe found problem , appears simple understanding issue.

overall appears way understanding use of $scope , $stateparams. since trying work solution ui-router way going off. quick reference.

$stateparams service populated current state's parameters. useful injecting own controllers or services access parameters. have 1 key per url parameter.  note: must injected controller directly attached desired state 

so controller method not right..

app.controller('machinescontroller', function ($scope, machineservice) {     $scope.data_table = machineservice.query(); }); 

using $stateparms, looks , works correctly.

app.controller('machinescontroller', function ($scope, $stateparams, machineservice) {     $scope.data_table = machineservice.get({hostname:$stateparams.hostname}); }); 

then factory can simplify it.

snip..   boxservices.factory('machineservice', ['$resource',       function($resource){         return   $resource('http://127.0.0.1:5000/v1.0/machineroute/:hostname',   {});     }; ..snip 

once these changes made able see api called correct parameter , retrieve data. in end, read docs(again!) , seek out others :)


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -