如何使用 angularjs $http.delete() 请求发送数据? [英] How to send data with angularjs $http.delete() request?

查看:32
本文介绍了如何使用 angularjs $http.delete() 请求发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资源角色",它与用户"有多对多的关系.要管理角色",我需要将角色 ID 和用户 ID 发送到服务器,以便它从正确的用户(不一定是登录用户)中删除角色

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)

这是我正在尝试的,但根据 docs 这是不可能的.我知道我可以在 uri 中发送两个 id,但我的 laravel 后端会自动设置资源/{resourceid} 的资源路由,如果可能的话我想使用它.有没有办法做到这一点,我错过了?

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

推荐答案

您可以通过/users/1/roles/2 之类的 URL 执行 http DELETE.这将是最 RESTful 的方式.

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

否则我猜您可以将用户 ID 作为查询参数的一部分传递?类似的东西

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...

这篇关于如何使用 angularjs $http.delete() 请求发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆