0

I'm trying to delete a record in angular 2, the API is working fine, but when I call the method who delete, the console says:

Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response.

Of course I have been reading about the error, but the most popular solution doesn't work for me, here is the method:

deleteInventory(id){
    var headers = new Headers();
    headers.append('Authorization', `Bearer ${this.globalVar.getToken()}`);
    headers.append("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, 
    DELETE");
    var options = new RequestOptions({ headers: headers });
    var result = this.http.delete(this.globalVar.getHost() + "inventory/"+ 
    id, options);
   return result;
}

Can anybody help me? Thank you :D

Marlaurita
  • 583
  • 3
  • 11
  • 26
  • 1
    how are you saying the API works? have you tried it to any 3rd party app like postman? the `Access-Control-Allow-Methods` should be in your server response I believed, since you should be allowing the client. – Roljhon Apr 02 '17 at 14:13
  • 3
    The headers need to be added to the response by the server. Adding these headers on the request of the client is entirely pointless. There about 1k such questions. Try searching again and you surely will find something. You need to change the configuration on the server. If you don't control the server, use your own server and forward requests to the 3rd-party server and add the headers when you return the response to the browser. – Günter Zöchbauer Apr 02 '17 at 14:15
  • http://stackoverflow.com/q/34790051/573032 – Roman C Apr 02 '17 at 14:29
  • @Roljhon I'm using Rest client, to test the api, and it dosen't need the Access control allow methods, but when I test in my project, it gave me the error about: **XMLHttpRequest cannot load** is for that reason I put the access control, simirlaly if I put it in the postman it's work. – Marlaurita Apr 02 '17 at 14:32
  • @GünterZöchbauer You mean that the header has to be placed in the API? I don't understand how it can works in the postman and not in my project in case it is so – Marlaurita Apr 02 '17 at 14:37
  • 1
    postman works differently, you should be adding the control on your server response. that's all i could think of – Roljhon Apr 02 '17 at 14:37

1 Answers1

0

The headers need to be added to the response by the server. Adding these headers on the request of the client is entirely pointless. There about 1k such questions. Try searching again and you surely will find something. You need to change the configuration on the server. If you don't control the server, use your own server and forward requests to the 3rd-party server and add the headers when you return the response to the browser.

Günter Zöchbauer

Marlaurita
  • 583
  • 3
  • 11
  • 26