Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a resource 'roles' which has a many to many relationship with 'user'. To administer 'roles' I need to send the role id and the user id to the server so that it removes the the role from the correct user (not necessarily the logged in user)

Here is what I was attempting but according to the docs this is not possible. I know I can send the two ids in the uri but my laravel backend automatically sets up a resourceful route of 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);         });     }; 
like image 241
Mark Avatar asked Feb 26 '14 22:02

Mark


People also ask

Can we send data in delete request?

By this it seems optional whether you want to provide a body for a DELETE request. The RFC states that: A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

How do I use HTTP request delete?

The HTTP DELETE method is used to delete a resource from the server. Unlike GET and HEAD requests, the DELETE requests may change the server state. Sending a message body on a DELETE request might cause some servers to reject the request. But you still can send data to the server using URL parameters.

How do we pass data and get data using http in angular?

get request Method Syntax: $http. get(url, { params: { params1: values1, params2:values2, params3:values3...... } });

What is Delete method in http?

The DELETE method requests that the origin server delete the resource identified by the Request-URI. This method MAY be overridden by human intervention (or other means) on the origin server.

How to delete a HTTP service in AngularJS?

AngularJS Http Delete ($http.delete()) Service Method. The $http.DELETE() method in angularjs is used to delete a resource on the server based on given specific URL. Its short cut method of $http services in angularjs. Generally $http.delete method rarely allowed to delete a resources on the server due to security reasons.

What is the use of $HTTP in angular?

It is used to define a required operator like get or send data. In angularjs we have different methods available. We need to send url of http server to perform required operations. In above syntax we passed $http as argument to the controller.

How do I delete a many to many relationship in angular?

$http ( angular.merge ( {}, config || {}, { method : 'delete', url : _url, data : _data })); A many to many relationship normally has a linking table. Consider this "link" as an entity in its own right and give it a unique id, then send that id in the delete request.

How does http delete data from the server?

But in a real-world application it soft deletes the data, meaning data is not be removed from the server permanently, it just gets inactivated, mainly for reporting purposes. To make the HTTP request, you should always look for the headers because in many cases the server will only accept the request from an authorized client.


2 Answers

You can do an http DELETE via a URL like /users/1/roles/2. That would be the most RESTful way to do it.

Otherwise I guess you can just pass the user id as part of the query params? Something like

$http.delete('/roles/' + roleid, {params: {userId: userID}}).then... 
like image 122
smbergin79 Avatar answered Sep 25 '22 05:09

smbergin79


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); }); 
like image 39
eldes Avatar answered Sep 25 '22 05:09

eldes