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

前端 未结 2 1147
执念已碎
执念已碎 2020-12-31 01:04

I am using jersey as my restful api implementation. In the front end, I am using angularjs $http service to make http request. When I request a delete method I always got be

相关标签:
2条回答
  • 2020-12-31 01:16

    Try using the CORS extension for chrome, it helped me once.

    EDIT

    This happens because angular adds X-Header keys in your request headers.

    X-Headers generally define or set the operating parameters for the HTTP req/res

    You can fix this by modifying the Content-Type of your requests to "text/plain" or "application/x-www-form-urlencoded" or "multipart/form-data".

    You can do this by using an interceptor while in your app config.

    EDIT

    Add this to your server code -

    header('Access-Control-Allow-Headers: X-Requested-With');
    

    Try changing the content-type to text/plain

    Hope this helps.

    0 讨论(0)
  • 2020-12-31 01:39

    After some testing, I found the solution. I put the allow method on the header as below, then it works. I don't know why "*" doesn't work.

    headers.add("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
    
    0 讨论(0)
提交回复
热议问题