Express 即使Access Control Allow Methods设置为允许方法删除,也不允许方法删除
我的中间件代码:Express 即使Access Control Allow Methods设置为允许方法删除,也不允许方法删除,express,proxy,delete-method,Express,Proxy,Delete Method,我的中间件代码: app.use(cors()); app.use(function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS"); res.header("Access-Control-Allow-Headers", "Origi
app.use(cors());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
if (req.method === 'OPTIONS') {
res.send(200);
} else {
next();
}
});
每次我发现有人有相同的错误消息时,纠正他们问题的响应包括告诉他们像我一样设置“访问控制允许方法”
我在前端的删除方法:
deleteSaved(url) {
// return axios.delete("https://api.mlab.com/api/1/databases/etc.", {
// params: {
// "url": url
// }
// })
// .then(function(results) {
// console.log("axios results", results);
// return results;
// });
return fetch("/saved", {
method: 'DELETE',
body: JSON.stringify({url: url}),
mode: 'cors',
cache: 'default'
}).then(function(response) {
return response;
}).then(function(data){
console.log(data);
return data;
});
}
我有fetch和axios,因为我认为我在使用axios delete方法时遇到了问题。在开发服务器中,两个调用都能正常工作
在axios方法中,为了简单起见,我有我的db API URL(使事情更容易进行);在fetch方法中,我使用了“/save”,因为上次我问这个问题时,有人评论并建议我通过express服务器代理所有DB请求,而不是让浏览器直接连接。我尝试了一些方法(将“proxy”和“https_proxy”设置为与createreact应用程序前端关联的package.json中的mlab api url),但我认为我并不真正理解这意味着什么。代理不起作用(“保存”将导致应用程序URL+/saved处的GET请求出错,而不是mlab API URL),我在google上找不到任何关于通过express服务器将请求代理到DB的信息
我的回复标题:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Methods:POST,GET,OPTIONS,PUT
Access-Control-Allow-Origin:https://nyt-mern-app.herokuapp.com
Access-Control-Max-Age:1728000
Allow:POST,GET,OPTIONS,PUT
Cache-Control:no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:0
Content-Type:text/html;charset=UTF-8
Date:Thu, 26 Oct 2017 19:34:08 GMT
Expires:Tue, 01 Feb 2000 08:00:00 GMT
Keep-Alive:timeout=5, max=99
Last-Modified:Thu, 26 Oct 2017 19:34:08 GMT
Pragma:no-cache
Server:Apache/2.4.7 (Ubuntu)
X-Frame-Options:DENY
正如我所说,一切都在本地开发环境中运行。我如何让它在部署中工作