Angular 在请求正文中发送 ID 而不是 URL
Angular sends the ID in the request body instead of URL
这是一个资源定义:
app.factory('Program', ['$resource', function ($resource) {
return $resource(host + '/rest/program/:id/:action', {}, {query: {method: 'get', isArray: false}});
}]);
当我调用 Program.save({id:3,name:'foo'});
时,它将请求发送到 POST /rest/program
并将 {id:3,name:'foo'}
放入请求正文中。它不应该将更新请求发送到 /rest/program/3
并仅将 {name:'foo'}
放入请求正文中吗?
https://docs.angularjs.org/api/ngResource/service/$resource
The action methods on the class object or instance object can be
invoked with the following parameters:
HTTP GET "class" actions: Resource.action([parameters], [success], [error])
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
non-GET instance actions: instance.$action([parameters], [success], [error])
由于您使用的是非 GET 操作并且只传入一个参数,因此它假定它是 postData
并且未提供可选参数参数。
使用:
Program.save({id:3}, {id:3, name:'foo'});
试试这个:
app.factory('Program', ['$resource', function ($resource) {
return $resource(host + '/rest/program/:id/:action', {
id: '@id',
action: '@action'
}, {
query: {
method: 'get',
isArray: false
}
});
}]);
您可能也需要将它们作为参数传递。
这是一个资源定义:
app.factory('Program', ['$resource', function ($resource) {
return $resource(host + '/rest/program/:id/:action', {}, {query: {method: 'get', isArray: false}});
}]);
当我调用 Program.save({id:3,name:'foo'});
时,它将请求发送到 POST /rest/program
并将 {id:3,name:'foo'}
放入请求正文中。它不应该将更新请求发送到 /rest/program/3
并仅将 {name:'foo'}
放入请求正文中吗?
https://docs.angularjs.org/api/ngResource/service/$resource
The action methods on the class object or instance object can be invoked with the following parameters:
HTTP GET "class" actions: Resource.action([parameters], [success], [error])
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
non-GET instance actions: instance.$action([parameters], [success], [error])
由于您使用的是非 GET 操作并且只传入一个参数,因此它假定它是 postData
并且未提供可选参数参数。
使用:
Program.save({id:3}, {id:3, name:'foo'});
试试这个:
app.factory('Program', ['$resource', function ($resource) {
return $resource(host + '/rest/program/:id/:action', {
id: '@id',
action: '@action'
}, {
query: {
method: 'get',
isArray: false
}
});
}]);
您可能也需要将它们作为参数传递。