用于post方法的Sails.js CORS

用于post方法的Sails.js CORS,sails.js,Sails.js,我能够使用如下GET请求为特定控制器使用CORS: 'get /url': { controller: 'somecontroller', action: 'someaction', cors: true }, sails.config.csrf.routesDisabled = '/url'; 但是,如果我尝试像下面这样使用POST,则会出现“拒绝访问”错误: 如何为post方法设置cors?通过有限的研究,我发现简单地在控制器中添加

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

'get /url': {
        controller: 'somecontroller',
        action: 'someaction',
        cors: true
    },
sails.config.csrf.routesDisabled = '/url';
但是,如果我尝试像下面这样使用POST,则会出现“拒绝访问”错误:


如何为post方法设置cors?

通过有限的研究,我发现简单地在控制器中添加“cors:true”并不能解决问题。Sails仍然希望post方法使用
csrf
令牌。在
config
文件夹下的
bootstrap.js
文件中,我在底部添加了以下代码,使用以下命令禁用路由上的
csrf
令牌:

'get /url': {
        controller: 'somecontroller',
        action: 'someaction',
        cors: true
    },
sails.config.csrf.routesDisabled = '/url';
如果您有更好的解决方案,请在这里发布。谢谢

编辑:您也可以在
config/csrf.js
文件中更改此选项。更改
module.exports.csrf=true至:

module.exports.csrf = {
    routesDisabled: '/url',
};
您可能需要在API中使用它

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

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

'get /url': {
        controller: 'somecontroller',
        action: 'someaction',
        cors: true
    },
sails.config.csrf.routesDisabled = '/url';
如果您有更好的解决方案,请在这里发布。谢谢

编辑:您也可以在
config/csrf.js
文件中更改此选项。更改
module.exports.csrf=true至:

module.exports.csrf = {
    routesDisabled: '/url',
};
您可能需要在API中使用它

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

在config/routes.js中,在声明路由之前放置:

'OPTIONS/*':函数(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'

})

在config/routes.js中声明路由前放置:

'OPTIONS/*':函数(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'
})