2

I used Angular 2 Release and using Http and Headers from @angular/http.

I try call verb delete, with Headers, and the others verbs POST, GET and PUT, all right. But with "DELETE", don't work.

My code :

remove(url:string, id:any) {
   let headers = new Headers();
   headers.append('Authorization': 'Bearer ' + this.token);
   return this.http.delete(url, {
        headers: headers;
        body: { id: id }
   }).map(response => response.json());
}

A response received is: the requested resource does not support http method 'delete'.

In my controller I have some like this:

[HttpDelete]
public Task<HttpResponseMessage> Delete(int id)
{
    //..
}

Thanks for help me, regards !!

jfreire
  • 41
  • 5

3 Answers3

2

The solution that worked for me was, use query string, in this way:

remove(urlController:string, id:any){
   let headers = new Headers();
   headers.append('Authorization': 'Bearer ' + this.token);
   return this.http.delete(urlController + '/?id=' + id, {
    headers: headers;
    }).map(response => response.json());
}

example:

remove(urlController:string, id:any){
   let headers = new Headers();
   headers.append('Authorization': 'Bearer ' + this.token);
   return this.http.delete('users'+ '/?id=' + '2', {
    headers: headers;
    }).map(response => response.json());
}
jfreire
  • 41
  • 5
0

The problem is that your API server have to allow the HTTP DELETE Method. When you perform an HTTP DELETE Request, your browser send first an HTTP OPTIONS to the server; the server respond with an header Access-Control-Allow-Methods that contain the list of methods allowed.

you have to send an OPTION Response Header like this:

Access-Control-Allow-Methods: DELETE

Read more here: CORS support for PUT and DELETE with ASP.NET Web API

Community
  • 1
  • 1
0

I use custom httpService extends from http and I faced the some problem, I just change url string, this is work for me:

this.httpService.delete(`${API_URL}Favorites/${id}`)
Burhan Mubarok
  • 358
  • 2
  • 8