角度$资源删除不会将body发送到express.js服务器 [英] angular $resource delete won't send body to express.js server
问题描述
hye,
我正在服务器端使用angular.js和node.js(Express.js)构建一个应用程序。
由于某些原因,我在处理删除请求时遇到问题。没有身体正在服务器端。
这是我的angular.js资源代码:
$ scope.deleteProject = function(projectName){
var postData = {username:'name',projectName:projectName};
Project.deleteProject.delete({},postData,
function(res){
alert('Project Deleted');
},
函数(err) {
alert(err.data);
});
}
在服务器端我有这个:
var deleteProject = function(req,res){
console.log(req.body);
console.log(req.params);
if(req.body.projectName){
// do something
return res.send(200);
}
else
return res.send(400,'没有指定项目名称');
}
现在由于某种原因,没有任何身体!它是空的
i将路由定义为app.delete。如果我将node.js中的路由更改为post,并在angular.js中保存,则该路由可以正常运行。
我在这里错过了什么(撞上我的头)。
谢谢。
根据这个堆栈溢出问题和 $ http
服务源代码,一个 DELETE
请求使用 $ http
不允许在请求正文中发送数据。 DELETE请求的规范对于是否有一个应该允许请求机构,但Angular不支持它。
允许请求机构的唯一方法是 POST
, PUT
和 PATCH
。所以这个问题并不在您的代码中,而是在Angular的 $ http
服务中。
我的建议是使用通用的 $ http(...)
函数并传递正确的方法$ {
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ',
data:{...},
标题:{'Content-Type':'application / json; charset = utf-8'}
})
hye,
i am building an app with angular.js and node.js (Express.js) on the server side.
for some reason i am having a problem handling a delete request. no body is getting to the server side.
this is my angular.js resource code:
$scope.deleteProject = function(projectName){
var postData = {username: 'name', projectName: projectName};
Project.deleteProject.delete({}, postData,
function(res){
alert('Project Deleted');
},
function(err){
alert(err.data);
});
}
on the server side i have this:
var deleteProject = function(req, res){
console.log(req.body);
console.log(req.params);
if (req.body.projectName){
//do something
return res.send(200);
}
else
return res.send(400, 'no project name was specified');
}
now for some reason there is no body at all!! it is empty. i have defined the route as app.delete.
if i change the route in node.js to post and in angular.js to save it works fine.
what am i missing here (banging my head).
thanks.
As per this stack overflow question and the $http
service source code, a DELETE
request using $http
does not allow for data to be sent in the body of the request. The spec for a DELETE request is somewhat vague on whether or not a request body should be allowed, but Angular does not support it.
The only methods that allow for request bodies are POST
, PUT
, and PATCH
. So the problem is not anywhere in your code, its in Angular's $http
service.
My suggestion would be to use the generic $http(...)
function and pass in the proper method:
$http({
method: 'DELETE',
url: '/some/url',
data: {...},
headers: {'Content-Type': 'application/json;charset=utf-8'}
})
这篇关于角度$资源删除不会将body发送到express.js服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!