我attemping调用通过$资源RESTful方法如下:
资源 :
angular.module('secure',['ngResource']).factory('Vehicle', function ($resource) {
return $resource('/secure/vehicle/index', { id: '@id' }, {
query: {
method: 'GET',
isArray: true
},
delete: {
method: 'DELETE',
isArray: false,
url: '/secure/vehicle/delete/:id'
}
});
});
然后,从其他服务我注入上述工厂,我呼吁这样DELETE方法:
factory.delete = function (procedureId) {
var vehicle = new Vehicle();
vehicle.$delete({id: procedureId}, function () {
//success
deferred.resolve();
}, function (errResponse) {
// fail
console.log(errResponse);
});
return deferred.promise;
};
( 注意不要递延的东西,它不会有或没有它的工作),不幸的是,我总是得到相同的答案:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/secure/vehicle/delete/21
Request Method:DELETE
Status Code:422 Unprocessable Entity
调用本身设置正确( 安全/车/删除/ 21)。 事实上,如果我做同样的,但而不是使用$资源变量,使用$ HTTP,一切正常!
$http({
'method': 'DELETE',
'url': '/secure/vehicle/delete/' + procedureId,
'headers': {
'Content-Type': 'application/json'
},
'data': ""
})
.success(function () {
// success
})
.error(function (data, status) {
console.log(data.errors);
});
所以,我想有些东西被省略$资源的方式,但什么? 任何帮助,将不胜感激!
编辑:
看来这是一个后端的问题时,它读取整个URL调用。 如果我把DELETE资源,使用$ HTTP,添加data: ""
如我上面,后端正确初始化。 但是,如果我尝试$资源三通,所需的PARAMS是预先配置的,并且不喜欢到后端,所以我需要找到要说到$资源如何添加类似的方式data: ""
,任何想法?
测试证明,它的工作原理:
angular.module('secure', ['ngResource']).factory('Vehicle', function($resource) { return $resource('/secure/vehicle/index', { id: '@id' }, { query: { method: 'GET', isArray: true }, delete: { method: 'DELETE', isArray: false, url: '/secure/vehicle/delete/:id' } }); }); angular.module('secure').factory('VehicleFactory', function(Vehicle, $q) { var factory = {} factory.delete = function(procedureId) { var deferred = $q.defer(); var vehicle = new Vehicle(); vehicle.$delete({ id: procedureId }, function(r) { deferred.resolve(r); }, function(errResponse) { console.log(errResponse); }); return deferred.promise; }; return factory; }); describe('VehicleFactory', function() { var $httpBackend, VehicleFactory beforeEach(module('secure')); beforeEach(inject(function(_$httpBackend_, _VehicleFactory_) { $httpBackend = _$httpBackend_ VehicleFactory = _VehicleFactory_ })) it('deletes vehicle - vehicle.$delete()', function() { var r = { data: 'correct response' } $httpBackend.when('DELETE', '/secure/vehicle/delete/123').respond(r) VehicleFactory.delete(123).then(function(response) { expect(response.data).toBe(r.data) }) $httpBackend.flush(); }); afterEach(function() { $httpBackend.verifyNoOutstandingExpectation() $httpBackend.verifyNoOutstandingRequest() }) })
<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" /> <script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-resource.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-mocks.js"></script>
有些更清洁的方式来写,而不$ Q删除功能:
angular.module('secure').factory('VehicleFactory', function(Vehicle) {
var factory = {}
factory.delete = function(procedureId) {
return (new Vehicle()).$delete({
id: procedureId
})
}
return factory;
});