When I send a delete request to a certain endpoint, for example with httpie from terminal like
http delete http://localhost:8181/admin/applications/uspecs
I get a valid behavior, as in { success: true }
as response body.
But when I do
fetch (
'http://localhost:8181/admin/applications/uspecs',
{ method: 'DELETE' }
)
.then(res => doSomethingWithResponse())
.catch(err => console.error(err))
In javascript code, then I get a
Fetch API cannot load http://localhost:8181/admin/applications/uspecs.
Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response.
error on the console. What am I missing? I am getting a valid methods list on options request.
Fetch also supports the POST method call. To do a POST request we need to specify additional parameters with the request such as method, headers, etc. In this example, we'll do a POST request on the same JSONPlaceholder and add a post in the posts. It'll then return the same post content with an ID.
The fetch() method: Fetch API comes with a fetch () method that allows you to fetch data from all sorts of different places and work with the data fetched. It allows you to make an HTTP request, i.e., either a GET request (for getting data) or POST request (for posting data).
You need to send the Access-Control-Allow-Methods
header containing the allowed methods. Currently your header is named methods
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With