Sau*_*ron 0 post ruby-on-rails authenticity-token
我已将POST请求路由构建为:
match '/getActivatedFriends',
to: 'requests#getActivatedFriends', via: 'post',
constraints: { friends_phone_number_csv: /([0-9]+,?)+/ }
Run Code Online (Sandbox Code Playgroud)
采取行动:
def getActivatedFriends
@results = BusinessUser.find_by_sql("SELECT
a.id
, a.username
, a.phoneNumber
FROM users a
WHERE phoneNumber in ('+params[:friends_phone_number_csv]+') and
removed = 0 and
is_user = 1;")
respond_to do |format|
format.html
format.json { render json: { friends_match: @results }}
end
end
Run Code Online (Sandbox Code Playgroud)
那应该返回匹配的用户的JSON对象。我已经用POSTMAN测试过:

但是返回的是指出无效真实性令牌的错误
如何重新配置以使此POST路由正常工作?
默认情况下,Rails CSRF protection在控制器中使用。它hidden_field通过真实性令牌添加到您的表单中。但是在您不使用表单的情况下,可以CSRF protection通过跳过before_action验证在控制器上禁用表单。添加到您的请求控制器顶部:
skip_before_filter :verify_authenticity_token
Run Code Online (Sandbox Code Playgroud)
或in Rails 4 and 5(这是同一命令):
skip_before_action :verify_authenticity_token
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1372 次 |
| 最近记录: |