【发布时间】:2019-12-16 12:58:30
【问题描述】:
我正在尝试获取一个使用 MEAN 堆栈的简单示例,但我注意到我的 DELETE 请求在 Firefox 中不起作用。我相信这是由于 CORS。
当我单击链接以向我的 ExpressJS 后端提交带有 angularJS 的 DELETE 请求时,我看到首先调用了 GET 请求,然后调用了实际的 DELETE 请求,但 DELETE 请求从未完成。
这一切似乎在 Chrome 中都可以正常工作。没有调用意外的 GET 请求,并且 DELETE 请求实际上完成了。如何在 Firefox 中进行这项工作?
cors 的快递部分:
// CORS Stuff in app.js
app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Authorization, Content-Length");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
if(req.method == 'OPTIONS') {
res.send(204);
//next();
} else {
next();
}
//res.send(200);
});
app.get('/notes', note.get(db));
app.post('/notes', note.create(db));
app.put('/notes/:id', note.update(db));
app.delete('/notes/:id', note.delete(db));
然后我有这个 note.js 文件
exports.delete = function(db) {
return function(req, res) {
console.log('were in delete middleware');
//var note_id = req.body.id;
var note_id = req.params.id;
console.log("note_id = "+note_id);
var collection = db.get('notes');
collection.remove(
{_id: note_id}
),
function(err, doc) {
console.log('were in delete middleware function');
// If it failed, return error
if (err) {
console.log('were in delete error');
res.send("There was a problem deleting that note from the database.");
} else {
console.log('were in delete success');
res.send(200);
}
}
}
}
我的浏览器网址是http://localhost:63342/express_example/public/#/
更新
我的服务器用这个来响应预检请求:
服务器控制台: 选项 /notes/53568807130e0f701f000001 200 0ms - 2b GET /notes 304 8ms
浏览器控制台:
【问题讨论】:
-
你的服务器用什么来响应预检请求?
-
我已经用预检请求详细信息更新了问题。
-
@Catfish 请发布您的删除处理程序的代码。正如您对 DELETE 响应 204 的示例所示,请求未被浏览器阻止,因此这对我来说不是 CORS 问题,而是错误的删除处理程序。
-
我已经用更多细节更新了问题中的代码。
标签: node.js angularjs express firefox