Marco Carrai
Marco Carrai

Reputation: 43

How to pass parameter to DELETE in AngularJS?

Which is the right way to pass parameter in a DELETE in AngularJS?

This call is not working:

 $http.delete("api/testDelete",
             { 
              Id:5
             }
             ).success(function (data) {
                alert('Exit status ' + data);
            });

This call is working:

  $http.delete("api/testDelete",
             { 
               { params: { Id: 5 } }
             }
             ).success(function (data) {
                alert('Exit status ' + data);
            });

This is the C# Web Api being called:

[HttpDelete]   
  public bool testDelete(int Id)
  { return true; }

Why do i have to use params? Is it possible to pass an object? Thanks

Upvotes: 1

Views: 1182

Answers (2)

Francisco Carmona
Francisco Carmona

Reputation: 1701

You can send params to the api in two ways:

  • In the query string:

    $http.delete("api/testDelete",{ 
         { params: { Id: 5 } }
     }).success(function (data) {
         alert('Exit status ' + data);
     });
    
  • In the body of the request:

     $http.delete("api/testDelete",{ 
         { data: { Id: 5 } }
     }).success(function (data) {
         alert('Exit status ' + data);
     });
    

Upvotes: 0

Daniel Tran
Daniel Tran

Reputation: 6171

You need to send the id in the URL:

$http.delete("api/testDelete/5")
     .success(function (data) {
        alert('Exit status ' + data);
     });

Upvotes: 2

Related Questions