Alessandro
Alessandro

Reputation: 925

How to send DELETE request to server with Json data using AngularJS?

I have to send a http DELETE request to a server. The type has to be JSON, and the object looks like this:

{ "id": "value"}

My first approach was the following code, but so far it doesn't work:

$http.delete('http://blabla/server/house', {"id": "value"}).success(function(data) {
            console.log(data);
            //Redirect to index.html
            $location.path('/'); 
        });

What would a working solution look like?

Upvotes: 4

Views: 15577

Answers (3)

Asher
Asher

Reputation: 403

As @KevinB pointed out, config is the second parameter.

var obj = { "id": "value"};
var config = { data: JSON.stringify(obj) };
$http.delete('http://blabla/server/house', config).success(function(data) {
        console.log(data);
        //Redirect to index.html
        $location.path('/'); 
    });

Upvotes: 4

Artyom Pranovich
Artyom Pranovich

Reputation: 6962

I guess you can just pass the param as part of the query params. Something like this:

var config = {
   params: {
     yourServerSideParamName: JSON.stringify({'id': 'value' })
   }
};

$http.delete('blabla/server/house', config).success(function(data){
   $location.path('/'); 
});

Hope it helps!

Upvotes: -1

mggSoft
mggSoft

Reputation: 1042

$http({
            method: 'DELETE',
            url: 'http://blabla/server/house',
            data: JSON.stringify({
                'id': 'value'
            })
        }).success(function (results) {
            console.log(results);
            //Redirect to index.html
            $location.path('/'); 
        });

Upvotes: 0

Related Questions