当我向我的 ruby Controller 发送 ajax 请求时,before_filter 函数似乎为每个 ajax 请求调用了两次。我使用 before_filter 进行身份验证。
我使用的ajax请求:
$.ajax({
type: 'GET',
url: 'URL_TO__RoR_Controller',
username: username,
password: password,
contentType: "application/json; charset=utf-8",
success: function(data){
console.log(data);
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(arguments);
}
});
RoR 代码:
class Api::MyController < ApplicationController
before_filter :authenticate
attr_reader :current_user
def authenticate
print "===================================\n"
authenticate_or_request_with_http_basic do |username, password|
print "\n--------------------------------\n"
print username, password
print "\n--------------------------------\n"
@current_user = User.authenticate(username, password)
print "===================================\n"
end
end
def show
print "=================IN SHOW=================="
end
end
对ajax请求的RoR响应:
Started GET "URL_TO__RoR_Controller" for 127.0.0.1 at 2015-10-08 10:18:18 +0200
Processing by Api::MyController#show as JSON
Parameters: {"id"=>"show", "test"=>{}}
===================================
===================================
Filter chain halted as :authenticate rendered or redirected
Completed 401 Unauthorized in 1ms (ActiveRecord: 1.0ms)
Started GET "URL_TO__RoR_Controller" for 127.0.0.1 at 2015-10-08 10:18:19 +0200
Processing by Api::MyController#show as JSON
Parameters: {"id"=>"show", "test"=>{}}
===================================
--------------------------------
USERPASS
--------------------------------
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`authorized` = 1 AND `users`.`login` = 'USER' LIMIT 1
===================================
=================IN SHOW==================
Rendered URL_TO__RoR_Controller/show.json.rabl (0.8ms)
Completed 200 OK in 30ms (Views: 3.4ms | ActiveRecord: 1.7ms)
RoR config/routes.rb for this single controller:
Api::Application.routes.draw do
namespace :api, defaults: { format: :json } do
resources :MyController, only: [:show]
end
end
所以基本上,对于我发送的每个 ajax 请求,身份验证功能似乎都执行了两次。第一次验证似乎失败了。它甚至没有进入 authenticate_or_request_with_http_basic
函数。第二次一切似乎都很好。
显然这不是我们想要的行为。是什么导致了这个问题?
编辑:
好的,这是我删除 before_filter 时的发现:
1) 当完全删除 before_filter 时,问题不再发生。
2) 当保留/使用 before_filter 但将 authenticate_or_request_with_http_basic 替换为“true”时,如下所示:
def authenticate
true
end
也没有出现这个问题。所以问题似乎是由 authenticate_or_request_with_http_basic 引起的?
最佳答案
除非以某种方式调用它,否则不会发生这种情况。
看到请求在彼此之间发送了 1 秒,这表明您的 Ajax 被发送了两次:
127.0.0.1 at 2015-10-08 10:18:18 +0200
127.0.0.1 at 2015-10-08 10:18:19 +0200
虽然这本身不是答案,但您可以通过多种方式“调试”它:
- Firstly, you must understand this will not happen on its own
- The "debug" process is simply a case of identifying what's happening & resolving it
- I don't think Rails will fire a controller action twice without it being invoked
因此,您必须查看问题的各个要素:
首先,您的Ajax 请求似乎被触发了两次。
您提供了以下代码:
$.ajax({
type: 'GET',
url: 'URL_TO__RoR_Controller',
username: username,
password: password,
contentType: "application/json; charset=utf-8",
success: function(data){
console.log(data);
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(arguments);
}
});
这不向我们展示它是如何绑定(bind)到您页面上的元素的。我推测您遇到了 Turbolinks 的问题。
调试的第一步是确保您的 JS 绑定(bind)不受阻碍。具体来说,您要确保利用 turbolinks
hooks (page:load
):
#app/assets/javascripts/application.js
bind = function() {
$(".element").on("click", function() {
// Ajax
});
};
$(document).on("ready page:load", bind);
或者您可以从文档中委派:
#app/assets/javascripts/application.js
$(document).on("click", ".element", function() {
// Ajax
});
其次,您的 Controller 操作似乎被调用了两次,但这不仅仅是 before_filter
...而是整个流程(在第二个实例中)。
Filter chain halted as :authenticate rendered or redirected
Completed 401 Unauthorized in 1ms (ActiveRecord: 1.0ms)
第一个 before_filter
没有触发的唯一原因是你没有通过身份验证。我不知道您是如何对用户进行身份验证的,但如果您能够做到这一点,我认为流程就会发生。
那么问题就变成了您如何让第一个请求被退回?
如果您在这两个请求之间有任何不同之处,我们需要看到它。不是我想你会知道,它是自动完成的:)
--
这可以通过观察 "network" requests sent to the server 得到支持使用 ajax:
最后,您想在不使用 before_filter
方法的情况下进行一些测试,看看是否可行。也许您没有获得正确的真实性 token 或中间件中的其他问题。
关于javascript - Ruby On Rails before_filter 调用了两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33010566/