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 !!

4
  • What's the url you're passing client side and the route on server side? Commented Nov 24, 2016 at 19:33
  • I also guess this might be some url mistake. Make sure you debug and see if the url for delete is correct. Also, is your api in .net core? Commented Nov 25, 2016 at 8:46
  • localhost:4200/api/user it's API REST in .net core. Commented Nov 25, 2016 at 12:27
  • does anyone able to find the issue yet? Commented Dec 10, 2016 at 6:55

3 Answers 3

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());
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.