[英]Devise ajax sign_in
我试图通过做一个AJAX sign_in devise
,我按照这个教程 。
我已经成功创建了一个ajax sign_up
。 这是我的registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
respond_to :json
skip_before_filter :verify_authenticity_token, :only => :create
end
它工作正常,但sign_in
失败。
这是我的session_controller.rb
class SessionsController < Devise::SessionsController
respond_to :json
end
参数和路径是这样的
路径: /users/sign_in
参数:
{
"user":
{
"email": "123@gmail.com",
"password":"12345678",
"remember_me":"1"
},
"commit":"Log in"
}
但是我得到了这个错误。
我仍然有401 Unauthorized
。 我尝试了以下方法。
1. 使用以下protect_from_forgery
将application_controller.rb的protect_from_forgery
为protect_from_forgery with: :null_session
skip_before_filter :verify_authenticity_token, :only => :create
在sessions_controller.rb中添加 skip_before_filter :verify_authenticity_token, :only => :create
3.修改我的ajax
到这个
$.ajax ({
headers: {
'X-Transaction': 'POST Example',
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
url: "sign_in",
type: "POST",
dataType: 'json',
data: {
"user": {"email": "example@gmail.com", "password": "password"}
},
success: function(msg){
console.log("Success");
},
error: function(msg) {
console.log("Errors");
}
});
但是,它们都引发了相同的错误。
Started POST "/users/sign_in" for ::1 at 2016-12-14 01:13:09 +0800
Processing by SessionsController#create as JSON
Parameters: {"user"=>{"email"=>"example4@gmail.com", "password"=>"[FILTERED]"}, "commit"=>"Log in"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'example4@gmail.com' ORDER BY `users`.`id` ASC LIMIT 1
Completed 401 Unauthorized in 2ms (ActiveRecord: 0.3ms)
我相信CSRF令牌丢失了……您正在关注的教程中也对此进行了说明: http : //blog.andrewray.me/how-to-set-up-devise-ajax-authentication-with-rails-4- 0 /#maketheajax请求
尝试在您的application_controller.rb
通过以下protect_from_forgery
编辑protect_from_forgery
至protect_from_forgery with: :null_session
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.