我想发送一些参数在我的服务器上执行GET,一个必须在我的URL(collabId
)中。
这是我的服务:
/* Service */
angular.module('app').factory('CollabService', function($resource) {
return $resource('/collabs/:collabId', {collabId:'@collabId'}, {
getCollabs: {
method: 'GET',
headers: {'Content-Type': 'application/json'},
isArray: true
}
});
});
这是我的控制器。我想在$scope.q
中发送参数,并在网址中包含collabId
。
我已经尝试了这一点,但它不起作用(400 Bad Request
):
/* Controller */
$scope.q = {
"collabId": 30;
"firstWeek": 1;
"nbWeeks": 4;
"year": 2015
};
CollabService.getCollabs($scope.q, {collabId: $scope.q.collabId},
function(col) { //Print result
console.log(col);
});
我想我在控制器中的查询语法中遗漏了一些内容。你能帮我找到错在哪里吗?
谢谢:)
答案 0 :(得分:0)
以前的解决方案中存在轻微错误。 我把它重新做了一点&能够让这个为我工作。
// Factory
angular.module('app')
.factory('CollabService', function($resource) {
return $resource('/collabs/:collabId',
{collabId: '@collabId'},
{query: {
method: 'GET',
headers: {'Content-Type': 'application/json'},
isArray: true
}
});
});
// Controller
var q = {};
q.collabId = 30;
...
// Query
CollabService.query({collabId: q.collabId}, function(promisedData) {
// Promised data
console.log(promisedData);
});
答案 1 :(得分:0)
这个很好用,你应该试试:
//服务
angular.module('app')
.factory('CollabService', function($resource) {
return $resource('/collabs/:collabId', {collabId:'@collabId'});
});
//控制器
scope.q = {};
$scope.q.collabId = 30;
$scope.q.firstWeek = 1;
$scope.q.nbWeeks = 4;
$scope.q.year = 2015;
CollabService.get($scope.q,
function(col) { //Print result
console.log(col);
});
答案 2 :(得分:0)
CollabService
出错。在这里传递整个$scope.q
对象:
CollabService.getCollabs($scope.q, {collabId: $scope.q.collabId},
function(col) { //Print result
console.log(col);
});
应该改为:
CollabService.getCollabs({collabId: $scope.q.collabId}, // <--
function(col) { //Print result
console.log(col);
});
答案 3 :(得分:-1)
这应该有效:
// Service
angular.module('app')
.factory('CollabService', function($resource) {
return $resource('/collabs/:collabId', {collabId:'@collabId'}, {
query: {
method: 'GET',
headers: {'Content-Type': 'application/json'},
isArray: true
}
});
});
// Controller
var q = {};
q.collabId = 30;
q.firstWeek = 1;
q.nbWeeks = 4;
q.year = 2015;
// Get
CollabService.get({collabId: q.collabId}, function(col) {
//Print result
console.log(col);
});
// Query
CollabService.query(q, function(col) {
//Print result
console.log(col);
});