如果可能,我需要一些帮助。我们正在构建一个内部Web应用程序,并决定使用AngularJS。一切都很顺利,但现在我们正在尝试将调用集成到一个Flask REST API中。以下是我的数据,并在我的API中被模拟。AngularJS GET请求烧瓶/ rest API
machine_data = {
"machine1": {
"machine_name": 'LX1host',
"machine_model": '12345678',
"machine_serial": '87654321'
}
}
我想在我的视图中传递一个变量来返回上述结构。这里是API方法。
@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
我看到电话进来,并在Flask端获得200,但我确实在JS控制台上看到一个错误,并指出。我一直在使用Google,但看起来可能有几件事。
ConsoleAgent: Error: [$resource:badcfg] http://errors.angularjs.org/1.3.15/$resource/badcfg?p0=query&p1=array&p2=object
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:6:417
at q.then.p.$resolved (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-resource.min.js:9:330)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:112:113
at n.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:126:15)
at n.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:123:106)
at n.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:126:293)
at l (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:81:240)
at M (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:85:342)
at 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
这是我的看法的片段。
<tr ng-repeat="machine in data_table.message | filter:searchText">
<td><a ui-sref="machineRoute({hostname: machine.hostname">machine.hostname </a></td>
我的角度模块和配置。
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'
})
});
控制器:
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}
});
}]);
我也可以拨打这个电话与httpie这是成功的,所以我敢肯定这是我的角设置,但我不知道如何让它正确传递变量。
http GET http://127.0.0.1:5000/v1.0/machineroute/ hostname=testhost
这是我从httpie调用返回的数据包。
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
}
在此先感谢..