简体   繁体   English

如何使用angularjs $http.delete() 请求发送数据?

[英]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)要管理“角色”,我需要将角色 ID 和用户 ID 发送到服务器,以便它从正确的用户(不一定是登录用户)中删除角色

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.我知道我可以在 uri 中发送两个 id,但是我的 laravel 后端会自动设置资源/{resourceid} 的资源丰富的路由,如果可能的话我想使用它。 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);
        });
    };

You can do an http DELETE via a URL like /users/1/roles/2.您可以通过像 /users/1/roles/2 这样的 URL 执行 http DELETE。 That would be the most RESTful way to do it.这将是最 RESTful 的方式。

Otherwise I guess you can just pass the user id as part of the query params?否则我想您可以将用户 ID 作为查询参数的一部分传递吗? Something like就像是

$http.delete('/roles/' + roleid, {params: {userId: userID}}).then...

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);
});

$http.delete method doesn't accept request body. $http.delete方法不接受请求正文。 You can try this workaround :你可以试试这个解决方法:

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

where in config you can pass config data like headers etc.config您可以传递配置数据,如标题等。

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.将此“链接”视为一个实体,并为其提供唯一 ID,然后在删除请求中发送该 ID。

You would have aa REST resource URL like /user/role to handle operations on a user-role "link" entity.您将拥有像 /user/role 这样的 REST 资源 URL 来处理对用户角色“链接”实体的操作。

I would suggest reading this url http://docs.angularjs.org/api/ngResource/service/$resource我建议阅读这个网址http://docs.angularjs.org/api/ngResource/service/$resource

and revaluate how you are calling your delete method of your resources.并重新评估您如何调用资源的删除方法。

ideally you would want to be calling the delete of the resource item itself and by not passing the id of the resource into a catch all delete method理想情况下,您希望调用资源项本身的删除,而不是将资源的 id 传递到 catch all delete 方法中

however $http.delete accepts a config object that contains both url and data properties you could either craft the query string there or pass an object/string into the data但是 $http.delete 接受一个包含 url 和 data 属性的配置对象,您可以在那里制作查询字符串或将对象/字符串传递到数据中

maybe something along these lines也许沿着这些路线

$http.delete('/roles/'+roleid, {data: input});

Please Try to pass parameters in httpoptions , you can follow function below请尝试在httpoptions传递参数,您可以按照下面的功能

deleteAction(url, data) {
    const authToken = sessionStorage.getItem('authtoken');
    const options = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        Authorization: 'Bearer ' + authToken,
      }),
      body: data,
    };
    return this.client.delete(url, options);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM