Reputation: 91
First, here is my middleware code that most other answers say fixes the concern:
//enable CORS
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
if (req.method === 'OPTIONS') {
res.send(200);
} else {
next();
}
});
I know for sure the GET and POST methods work, but anytime I try to delete something on my site, I get an error. Here is my delete method:
deleteSaved(title, data, url) {
return axios.delete("https://api.mlab.com/api/1/databases/daniels-first-database/collections/nytcollection?apiKey=*APIKEYHERE*", {
params: {
"title": title,
"data": data,
"url": url
}
})
.then(function(results) {
console.log("axios results", results);
return results;
});
}
Everything works on a local development server, so I don't think it has anything to do with my method, but there it is just in case.
Upvotes: 0
Views: 2449
Reputation: 770
On the response header of your server API, add:
"Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"
Upvotes: 1