两个ajax请求多次执行Rails控制器

时间:2015-07-28 21:20:39

标签: javascript ruby-on-rails ajax ruby-on-rails-4

我有一个ajax函数故意连续两次运行(带有不同的参数),但是在Rails中,我接收的是8-14个服务器调用,分为两组参数

  • 执行的次数似乎从请求变为请求。
  • 我已经通过控制台消息确认Javascript正在执行两次。
  • 两个ajax函数可以非常快速地发生,只需<1s。

的Ajax:

function receiveData(args) {
  var payload = args[0];
  var context = payload.id;
  var feedback = payload.value;

  $.ajax({
    url: "<%= generate_response_url(@program.id) %>/" + feedback
  }).done(function(output) {
    var arr = JSON.parse(output);
  });
}

路线:

get 'requests/generate_response/:program_id' => 'requests#generate_response', as: 'generate_response'

请求控制器:

before_action :require_user

def generate_response
  ...some data processing...
  render text: @message
end

服务器日志:

# Received happens 4-7 times then Complete happens 4-7 times

Started GET "/requests/generate_response/5/42/received" for ::1 at 2015-07-28 17:09:04 -0400
Processing by RequestsController#generate_response as */*
  Parameters: {"program_id"=>"5", "request_id"=>"42", "message"=>"received"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Company Load (0.1ms)  SELECT  "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT 1  [["id", 1]]
  Program Load (0.1ms)  SELECT  "programs".* FROM "programs" WHERE "programs"."company_id" = $1 AND "programs"."id" = $2 LIMIT 1  [["company_id", 1], ["id", 5]]
  Request Load (0.1ms)  SELECT  "requests".* FROM "requests" WHERE "requests"."user_id" = $1 AND "requests"."id" = $2 LIMIT 1  [["user_id", 1], ["id", 42]]
*** @message: ["received", "Program has begun processing your request."] ***
   (0.1ms)  BEGIN
  SQL (0.1ms)  UPDATE "requests" SET "custom" = $1, "response_time" = $2, "updated_at" = $3 WHERE "requests"."id" = $4  [["custom", "\"times_called\"=>\"6\""], ["response_time", "4.83"], ["updated_at", "2015-07-28 21:09:04.819523"], ["id", 42]]
   (0.2ms)  COMMIT
*** @message: ["received", "Program has begun processing your request."] ***
  Rendered text template (0.0ms)
Completed 200 OK in 6ms (Views: 0.2ms | ActiveRecord: 1.0ms)


Started GET "/requests/generate_response/5/42/complete" for ::1 at 2015-07-28 17:09:06 -0400
Processing by RequestsController#generate_response as */*
  Parameters: {"program_id"=>"5", "request_id"=>"42", "message"=>"complete"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Company Load (0.1ms)  SELECT  "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT 1  [["id", 1]]
  Program Load (0.1ms)  SELECT  "programs".* FROM "programs" WHERE "programs"."company_id" = $1 AND "programs"."id" = $2 LIMIT 1  [["company_id", 1], ["id", 5]]
  Request Load (0.2ms)  SELECT  "requests".* FROM "requests" WHERE "requests"."user_id" = $1 AND "requests"."id" = $2 LIMIT 1  [["user_id", 1], ["id", 42]]
*** @message: ["complete", "Your program is complete."] ***
   (0.1ms)  BEGIN
  SQL (0.2ms)  UPDATE "requests" SET "custom" = $1, "completion_time" = $2, "updated_at" = $3 WHERE "requests"."id" = $4  [["custom", "\"times_called\"=>\"11\""], ["completion_time", "6.35"], ["updated_at", "2015-07-28 21:09:06.341278"], ["id", 42]]
   (0.2ms)  COMMIT
*** @message: ["complete", "Your program is complete."] ***
  Rendered text template (0.0ms)
Completed 200 OK in 6ms (Views: 0.2ms | ActiveRecord: 1.1ms)

0 个答案:

没有答案