Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

node.js - angular $resource delete won't send body to express.js server

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...