Sails.js post 方法的 CORS

Sails.js CORS for post method

我能够使用如下所示的 GET 请求为特定控制器使用 CORS:

'get /url': {
        controller: 'somecontroller',
        action: 'someaction',
        cors: true
    },

但是,如果我尝试像下面这样使用 POST,我会收到 "Access Denied" 错误:

'post /url': {
            controller: 'somecontroller',
            action: 'someaction',
            cors: true
        },

如何为 post 方法设置 cors?

根据我有限的研究,我发现简单地向控制器添加 "cors: true" 并不能解决问题。 Sails 仍然期待 post 方法的 csrf 标记。在 config 文件夹下的 bootstrap.js 文件中,我在底部添加了以下代码,使用以下代码禁用路由上的 csrf 令牌:

sails.config.csrf.routesDisabled = '/url';

如果您有更好的解决方案,请post在此处提出。谢谢!

编辑:您也可以在 config/csrf.js 文件中更改它。将 module.exports.csrf = true; 更改为:

module.exports.csrf = {
    routesDisabled: '/url',
};

您可能需要将它用于您的 API

module.exports.csrf = {
    routesDisabled: '/api/*',
};

在 config/routes.js 中声明路由之前 put :

'OPTIONS /*': function(req, res) { res.send(200); },

并在 config/cors.js 中尝试放置 :

module.exports.cors = {

allRoutes: true,

origin: '*',

credentials: true,

methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',

headers: 'content-type, access-control-allow-origin, authorization'

};