首先,这是我的中间件代码,其他大多数答案都说明了这个问题:
//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();
}
});
我确信GET和POST方法有效,但是当我尝试删除网站上的内容时,我收到错误。这是我的删除方法:
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;
});
}
一切都在本地开发服务器上运行,所以我认为它与我的方法没有任何关系,但它只是以防万一。
答案 0 :(得分:0)
在服务器API的响应标头上,添加:
"Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"