I am trying to make a PUT
call to my rest api endpoint, and getting this error:
Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.
I enabled CORS
using this solution: enable-cors, it works for POST
.
How do I achieve the same for PUT
?
Thanks.
You will need to support the OPTIONS method on your server because the browser will pre-flight all cross-origin PUT requests, no matter what headers you have. And, you need to make sure you're explicitly allowing PUT in your CORS headers.
To solve this error, we need to add the CORS header to the server and give https://www.section.io access to the server response. Include the following in your index. js file. const cors = require('cors'); app.
CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options.
add this:
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
next();
});
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