使用主体发送Angular $ http.delete [英] Send Angular $http.delete with body
问题描述
在我的Angular应用中,我需要发送 $ http.delete
请求此路由/projects/:id/activityTypes
(请注意,它并不以活动结尾类型id),传递带有以下格式的正文:
[{"id":2}]
这是通过在数组内发送多个对象来允许批量删除操作.
使用Rest Web Service客户端测试上述路由,我得到的状态为 200,确定
,因此该路由已经实现并且可以正常工作,我只需要在Angular应用中实现它即可.
现在,
数据似乎已发送,但是我收到此控制台错误,在使用Rest Client时没有收到此错误:
对象{数据:"Json无效,无法从Project中删除活动类型",状态:400,配置:对象,statusText:错误请求"}
尝试一下:
$ http({方法:删除",网址:"projects/" + projectID +"/activityTypes",数据:[{id:2}]},标头:{'内容类型':'应用程序/json; charset = utf-8'});
In my Angular app, I need to send an $http.delete
request this route /projects/:id/activityTypes
(note it does not end with an activity type id) passing a body with the following format:
[{"id": 2}]
This is to allow batch delete operations by sending multiple objects within the array.
Testing the route above with Rest Web Service Client I get a 200, OK
status so the route is already implemented and working fine, I just need to implement it in my Angular app.
Now, I've checked this previous post which has a very similar question and learned that angular's $http.delete
does not send a body in the request by default, but that:
[it] does allow us to supply a config object as the second parameter, through which we can get access to the 'data' property.
So I tried what was suggested and sent a config object in my request:
return $http.delete('projects/' + projectID + '/activityTypes', {data: [{id: 2}]})
However, it does not work. It seems that although I am sending this config object, I am still not sending a body.
How can I actually pass a body to my $http.delete
route?
EDIT: Here is what the server is logging when I send the $http.delete
request above:
The data appears to have been sent, but I get this console error which I don't get when using the Rest Client:
Object {data: "Json not valid to remove activity type from Project", status: 400, config: Object, statusText: "Bad Request"}
try this:
$http({
method: 'DELETE',
url: 'projects/' + projectID + '/activityTypes',
data: [{id: 2}]},
headers: {
'Content-type': 'application/json;charset=utf-8'
});
这篇关于使用主体发送Angular $ http.delete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!