我正在使用Tower.js 0.4.2-20。
我发送了HTML FORM标记的GET和POST请求 'GET'请求是没有问题的回应。 但'POST'请求是返回以下错误。
我缺少路由器设置?
routes.coffee:
Tower.Route.draw ->
@resources 'service'
@match '/service/add', to: 'service#add', via: 'post'
ServiceController.coffee:
class ServiceController extends App.ApplicationController
index: ->
@render text: "GET"
create: ->
@render text: "POST"
add: ->
@render text: "ADD"
index.html:
<form method='POST' action='service/add'>
<div><input type='submit' value='Add'></div>
</form>
错误:
Error: Forbidden
at Object.exports.error (/var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/lib/utils.js:44:13)
at Object.module.exports [as handle] (/var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/lib/middleware/csrf.js:54:41)
at next (/var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at multipart (/var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/lib/middleware/multipart.js:57:27)
at module.exports (/var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:57:9)
at IncomingMessage.module.exports (/var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:68:11)
at IncomingMessage.EventEmitter.emit (events.js:115:20)
at Object.resume (/var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/node_modules/pause/index.js:25:18)
at store.get.next (/var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/lib/middleware/session.js:311:15)
at /var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/lib/middleware/session.js:333:9
答案 0 :(得分:6)
首先猜测:您的表单的相对URL是问题所在。将您的form
的{{1}}属性更改为action
,看看是否有帮助。您获得的403禁止错误可能意味着POST请求将转到您期望的其他URL路径。例如,如果您的表单位于/service/add
,则表单会因相对网址提交给/pages/service
。
/pages/service/service/add
at Object.module.exports [as handle] (/var/www/html/melissa/manage/development/yoshiaki-tanaka/node_modules/tower/node_modules/express/node_modules/connect/lib/middleware/csrf.js:54:41)
模块可能正在检查表单中的跨站点请求伪造令牌,其形式为crsf
标签(或类似名称)。阅读您正在使用的CRSF中间件上的文档,了解如何创建包含CRSF令牌的正确<input type="hidden" name="crsf_token" value="YOUR_CRSF_TOKEN">
标记HTML。