CHAD.DING CHAD.DING - 1 month ago 18
AngularJS Question

angular $resource send request with DELETE method and request body get 415 error

I send a delete quest via angular $resource, however it always get 415 error and the request body was turn to string looks like a get request.

'use strict';

(function(angular, window){

var authsys = angular.module('authsysApp');

authsys.factory('$_privilege', ['$resource', '$q', '$notify', function($resource, $q, $notify){

var resource = {
//删除权限
batchRemovePrivilege: $resource(window.ctxPath + '/rolepri/batchDeletePrivilegeFromRole', {}, {delete:{method: 'DELETE'}})
};

return {
batchRemovePrivilege: batchRemovePrivilege
};

function batchRemovePrivilege(params){

var q = resource.batchRemovePrivilege.delete(params).$promise;
return q.then(function(){
return true;
});
}


}]);

})(angular, window);


enter image description here

Tal Tal
Answer

Try to add a header to your request:

var resource = {
    //删除权限
    batchRemovePrivilege: function(params){
           return $http({
                method: 'DELETE',
                url: window.ctxPath + '/rolepri/batchDeletePrivilegeFromRole',
                data: params,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
           });
    }
};

execute:

var q = resource.batchRemovePrivilege(params).$promise;