AngularJs http方法重写PUT-POST
有没有办法在请求中使用AngularJs http方法重写PUT-POST,angularjs,http-method,x-http-method-override,Angularjs,Http Method,X Http Method Override,有没有办法在请求中使用X-http-method-override或\u方法参数使用angular的$resource服务执行http方法覆盖?在资源工厂中,您可以为每种类型的请求指定方法 angular.module('myServices', ['ngResource']) .factory('Customer', function($resource){ return $resource('../api/index.php/customers/:id', {id:'@id'
X-http-method-override
或\u方法
参数使用angular的$resource
服务执行http方法覆盖?在资源工厂中,您可以为每种类型的请求指定方法
angular.module('myServices', ['ngResource'])
.factory('Customer', function($resource){
return $resource('../api/index.php/customers/:id', {id:'@id'}, {
update: {method:'PUT'}
});
})
是标准方法,但您也可以使用:
angular.module('myServices', ['ngResource'])
.factory('Customer', function($resource){
return $resource('../api/index.php/customers/:id', {id:'@id'}, {
update: {params: {'_method':'PUT', id: '@id'}}
});
})
如果其他人正在查找代码段,请参见:
(function(module) {
function httpMethodOverride($q) {
var overriddenMethods = new RegExp('patch|put|delete', 'i');
return {
request: function(config) {
if (overriddenMethods.test(config.method)) {
config.headers = config.headers || {};
config.headers['X-HTTP-Method-Override'] = config.method;
config.method = 'POST';
}
return config || $q.when(config);
}
};
}
module.factory('httpMethodOverride', httpMethodOverride);
module.config(function($httpProvider) {
$httpProvider.interceptors.push('httpMethodOverride');
});
})(angular.module('app-module'));
非常感谢。我已经解决了这个问题,使用一个请求拦截器来转换请求,这样我就不需要更改已经编写的代码,我可以禁用它对拦截器进行注释