Sails.js Sails 1.0 POST route给出403错误
在我的routes.js文件中,我定义了如下路由:Sails.js Sails 1.0 POST route给出403错误,sails.js,Sails.js,在我的routes.js文件中,我定义了如下路由: module.exports = { /** * `IcalController.getEvents()` */ getEvents: async function (req, res) { console.log("hit here"); let events = []; for (var i = 0; i < 20; i++) { events.push({foo: "bar
module.exports = {
/**
* `IcalController.getEvents()`
*/
getEvents: async function (req, res) {
console.log("hit here");
let events = [];
for (var i = 0; i < 20; i++) {
events.push({foo: "bar" + i});
}
return res.json({
events: events
});
}
};
'post /get_some_records': {
action: 'home/get_some_records',
csrf: false
}
'PUT/api/v1/entry/login':{action:'entry/login'},
'POST/api/v1/entry/signup':{操作:'entry/signup'},
'POST/api/v1/entry/send password recovery email':{操作:'entry/send password recovery email'},
'POST/api/v1/entry/update password and login':{操作:'entry/update password and login'},
'POST/api/v1/deliver contact form message':{action:'deliver contact form message'},
“POST/api/v1/getEventsForICalUrl”:“IcalController.getEvents”,
我刚刚使用了默认生成的代码,并为geteventsforicalur
添加了最后一个路由。
我在controllers目录中创建了一个IcalController
,它有一个actiongetEvents
,它只是呈现一个json,如下所示:
module.exports = {
/**
* `IcalController.getEvents()`
*/
getEvents: async function (req, res) {
console.log("hit here");
let events = [];
for (var i = 0; i < 20; i++) {
events.push({foo: "bar" + i});
}
return res.json({
events: events
});
}
};
'post /get_some_records': {
action: 'home/get_some_records',
csrf: false
}
我的“已登录”策略文件如下:
module.exports = async function (req, res, proceed) {
// If `req.me` is set, then we know that this request originated
// from a logged-in user. So we can safely proceed to the next policy--
// or, if this is the last policy, the relevant action.
// > For more about where `req.me` comes from, check out this app's
// > custom hook (`api/hooks/custom/index.js`).
console.log("req.me=" + req.me);
if (req.me) {
return proceed();
}
//--•
// Otherwise, this request did not come from a logged-in user.
return res.unauthorized();
};
我只是把console.log放在这个文件中。其他的则与默认情况下从sailsnew
生成的相同
日志显示,使用POST时,这一个也不会被命中。(我没有在console.logs中看到“req.me=“..”),但这一个在使用get时会被命中
该路由似乎不适用于POST请求。我想知道这是sails js本身的错误还是我做错了什么。至少有两种方法可以解决这个问题。
可能您正在使用csrf。如果您这样做,您的配置可能包括以下内容:
module.exports.security = { // With Sails <1.01 this probably is in another file
csrf: true
};
因此,要在前端获取数据,您只需:
function get_some_records(object_with_data) {
$.get("/csrfToken", function (data, jwres) {
if (jwres != 'success') { return false; }
msg = {
some_data: object_with_data,
_csrf: data._csrf
};
$.post("get_some_records", msg, function(data, status){});
});
}
但是,如果你在做一些背景工作,Sails不会轻易给你csrf(可能有一些方法)。因此,您只需创建一条如下所示的路线:
module.exports = {
/**
* `IcalController.getEvents()`
*/
getEvents: async function (req, res) {
console.log("hit here");
let events = [];
for (var i = 0; i < 20; i++) {
events.push({foo: "bar" + i});
}
return res.json({
events: events
});
}
};
'post /get_some_records': {
action: 'home/get_some_records',
csrf: false
}
您可能使用Rest客户机进行测试(如Postman)。只需禁用csrf保护。在/config/security.js文件中
csrf: false
您是否尝试过从浏览器获取请求和从postman获取帖子?我尝试过从sails js前端发布帖子(使用axios库)。还检查了chrome@hamzafatmi中的网络流量,我想当您在执行
POST
时,可能没有发送请求中的cookie,因为在执行GET
时,这是自动为您完成的。您如何通过提交表单来提出post请求?@Logan_B您不应该:我在该页面上两次强调了这个想法。我添加了这个,但我仍然被禁止,但是,这在post中确实有效,您必须在客户端做些什么才能使其有效?postman可以工作,但从angular 6客户端来看,即使体内有csrftoken,仍然会出现403错误