How to send data using angularjs $ http.delete () request?

I have a role resource that has many, many relationships with a user. To administer the "roles", I need to send the role identifier and user ID to the server in order to remove this role from the correct user (not necessarily a registered user).

Here is what I tried, but according to docs this is not possible. I know that I can send two identifiers in uri, but my laravel backend automatically sets the resourceful route of the resource / {resourceid}, which I would like to use if possible. Is there a way to do this that I am missing?

var removeRole = function (roleid, userid) { var input =[]; input.user = userid; $http.delete('/roles/' + roleid, input).success(function (data, status) { console.log(data); }); }; 
+46
javascript angularjs laravel-4
Feb 26 '14 at 22:52
source share
5 answers

You can do http DELETE using a url like / users / 1 / role / 2. This would be the most RESTful way to do this.

Otherwise, I think you can just pass the user id as part of the request parameters? Something like

 $http.delete('/roles/' + roleid, {params: {userId: userID}}).then... 
+82
Feb 26 '14 at 22:58
source

My suggestion:

 $http({ method: 'DELETE', url: '/roles/' + roleid, data: { user: userId }, headers: { 'Content-type': 'application/json;charset=utf-8' } }) .then(function(response) { console.log(response.data); }, function(rejection) { console.log(rejection.data); }); 
+20
Jul 18 '16 at 15:09
source

Many, many relationships usually have a link table. Consider this "link" as an independent entity and give it a unique identifier, and then send this identifier to the deletion request.

You will have a REST resource URL, such as / user / role, to handle operations in the user's link object.

+2
Feb 26 '14 at 23:04
source

I would suggest reading this url http://docs.angularjs.org/api/ngResource/service/$resource

and overestimate how you invoke your method of deleting your resources.

Ideally, you would like to force the removal of the resource element itself and not pass the resource identifier to the delete all catch method

however, $ http.delete accepts a configuration object that contains both url properties and data, you could either process the query string or pass the object / string to the data

maybe something like that

 $http.delete('/roles/'+roleid, {data: input}); 
+2
Feb 26 '14 at 23:08
source

$http.delete method does not accept the request body. You can try this workaround:

 $http( angular.merge({}, config || {}, { method : 'delete', url : _url, data : _data })); 

where in config you can pass configuration data like headers, etc.

+2
Apr 11 '17 at 11:31 on
source



All Articles