Receive parameter in delete request

6

I am trying to pass a parameter to my DELETE request, I tried to do the following:

Use the same way I use in GET (where it works normal).

app.delete('/contatos', function(req, res){
    var obj = req.body;
    console.log(obj);
});

However in the NodeJS console this .log() prints only {} .

I've also tried this way (I found it in SOen ):

app.delete('/contatos',function(req, res){
    var obj = req.body.data;
    console.log(obj);
});

But the same thing happened. Maybe the DELETE implementation is different from GET and POST and I skipped some step.

I am requesting the API from AngularJS, with the following service:

angular.module("lista").factory("contatosAPI", function($http){     
    var _saveContato = function(contato){
        return $http.post("http://localhost:3412/contatos", contato);
    };

    var _deleteContato = function(contato){
        console.log(contato); //Aqui o objeto está normal
        return $http.delete("http://localhost:3412/contatos", contato);
    };

    return {
        saveContato: _saveContato,
        deleteContato: _deleteContato
    };
});

controller I call the service, like this:

$scope.apagarContatos = function(contato){      
    contatosAPI.deleteContato(contato);
};
    
asked by anonymous 01.09.2015 / 04:51

1 answer

2

The delete service of the $ http method, returns you a promise ie to access data from the server, it should be implemented as follows.

The delete service does not accept an OBJECT to be sent, because the object in the end performs a Get to the server, ie the information must go in the URL, so you can only pass parameters, not a complete object.

 $http.delete("http://localhost:3412/contatos/" + contato.ID).success(function (data, status) {
                console.log(data); // Retorno seu Data
            });
    
01.09.2015 / 14:07