【发布时间】:2018-05-01 20:35:03
【问题描述】:
在我的 routes.js 文件中,我定义了这样的路由:
'PUT /api/v1/entrance/login': { action: 'entrance/login' },
'POST /api/v1/entrance/signup': { action: 'entrance/signup' },
'POST /api/v1/entrance/send-password-recovery-email': { action: 'entrance/send-password-recovery-email' },
'POST /api/v1/entrance/update-password-and-login': { action: 'entrance/update-password-and-login' },
'POST /api/v1/deliver-contact-form-message': { action: 'deliver-contact-form-message' },
'POST /api/v1/getEventsForICalUrl': 'IcalController.getEvents',
我刚刚使用了默认生成的代码并添加了getEventsForIcalUrl 的最后一条路由。
我在控制器目录中创建了一个IcalController,它有一个动作getEvents,它简单地呈现一个像这样的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
});
}
};
我的问题是,每当我尝试从客户端访问此控制器时,都会出现 403 禁止错误。 当我将路由从 POST 更改为 GET 时,它按预期工作(我正在使用来自客户端的正确 GET/POST 请求作为路由)。
不确定是什么问题。 我还检查了日志。当我使用 GET 时,它的打印“点击这里”。 在我的策略文件中看起来像这样(因为它是生成的。我没有更改它):
module.exports.policies = {
'*': 'is-logged-in',
// Bypass the `is-logged-in` policy for:
'entrance/*': true,
'account/logout': true,
'view-homepage-or-redirect': true,
'deliver-contact-form-message': true,
};
我的“已登录”策略文件是这样的:
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 放在这个文件中。其他的则默认从sails new生成。
日志显示,使用 POST,这个也没有被命中。(我在 console.logs 中没有看到“req.me =”..)但是使用 GET 时这个被命中。
该路由似乎不适用于 POST 请求。我想知道它是sails js本身的错误还是我做错了什么。
【问题讨论】:
-
你试过浏览器的
GET请求和邮递员的POST吗? -
我从我的sails js 前端(使用axios 库)尝试了POST。还检查了 chrome @HamzaFatmi 中的网络流量
-
我认为当您执行
POST时,您可能没有将cookie 与请求一起发送,因为从浏览器执行GET时,它会自动为您完成。 -
如何通过提交表单来提出post请求?
-
@Logan_B 你不应该:我在那个页面上两次强调这个想法。
标签: sails.js