【问题标题】:Why does my 'POST' request NOT call my method, even though it sends something?为什么我的“POST”请求不调用我的方法,即使它发送了一些东西?
【发布时间】:2014-05-19 06:35:51
【问题描述】:

我正在尝试为如下所示的控制器方法请求“POST”:

$.ajax({type: "POST", url: something, data: {to_find_id: 978636122}, success:function(){ }});

它发送我的数据,显示以下日志:

Started POST "/thingys/519716477/do_something" for 127.0.0.1 at 2014-04-03 14:54:44 +0200
Processing by ThingysController#do_something as */*
  Parameters: {"to_find_id"=>"978636122", "thingy_id"=>"519716477"}
  ←[1m←[36mUser Load (0.0ms)←[0m  ←[1mSELECT "users".* FROM "users" WHERE "users"."id" =? LIMIT 1←[0m  [["id", 702273327]]
Redirected to http://localhost:3000/
Completed 302 Found in 0ms (ActiveRecord: 0.0ms)

302 Found的简短解释: WIKIPEDIA-LINK

我的方法:

def do_something
  Something.create(timespan_id: params[:to_find_id].to_s, thingy_id: @thingy.id)
  respond_to do |format|
    format.js {render :nothting => true}
  end
end

我已经在我的routes.rb-文件中定义了我的方法。

resources :thingys do
  post 'do_something'
  post 'undo_something'
end

所以它发送请求,甚至找到方法。

你知道,我该怎么做才能让它发挥作用吗?


编辑:

我已添加我的 Authenticity Token,但我仍然无法访问我的方法。

Here is what I get now:
Started POST "/thingys/519716477/add_something?&authenticity_token=1l3tTWfwNgt56lp93w2QghUJvUYZn87V/8Zs7Fcrijq+8=" for 127.0.0.1 at 2014-04-07 13:04:15 +0200
Processing by ThingysController#add_something as */*
  Parameters: {"to_find_id"=>"310026847", "authenticity_token"=>"1l3tTWfwNgt56lp93w2QghUJvUYZn87V/8Zs7Fcrijq 8=", "subgroup_id"=>"519716477"}
  ←[1m←[35mUser Load (0.5ms)←[0m  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 702273327]]
  ←[1m←[36mThingyp Load (0.5ms)←[0m  ←[1mSELECT "subgroups".* FROM "thingys" WHERE "thingys"."id" = ? LIMIT 1←[0m  [["id", "519716477"]]
Redirected to http://localhost:3000/
Completed 302 Found in 22ms (ActiveRecord: 1.0ms)

我还在我的 ApplicationController 中添加了protect_from_forgery with: :exception,在我的 ThingysController 中添加了skip_before_filter:verify_authenticity_token

【问题讨论】:

  • 什么不起作用?你能指望什么?会发生什么?

标签: jquery ruby-on-rails ruby ajax post


【解决方案1】:

如果没有 Rails 中的真实性令牌,您将无法在 Rails 中发送 POST 参数。

请参阅本页中的 CSRF 对策: http://guides.rubyonrails.org/security.html

或者到这个线程 Understanding the Rails Authenticity Token

【讨论】:

  • 如何通过我的 Ajax 请求将真实性令牌发送到我的应用程序?
  • 谢谢你,我已经得到了我的真实性代码,但它仍然不起作用。我已将新日志放入我的 EDIT 中
  • 您是否在被调用的控制器中添加了 'protect_from_forgery secret: "your code" '?
  • 我在我的 ApplicationController 中添加了它,并在我的 ThingysController 中使用了 skip_before_filter:verify_authenticity_token
【解决方案2】:

我假设“不起作用”意味着该操作没有执行您期望的任务,即创建 Something 实例。如果我不得不猜测,那么某些东西无法通过模型上定义的验证。这将阻止模型保存。

您可以尝试:Something.create!(attrs),如果验证失败,这将引发错误,从而更清楚为什么不保存。

要让它工作,你只需要保存一个有效的Something,不管这对你的应用程序意味着什么。


你也有错别字

format.js {render :nothting => true}
#                      ^ typo!

【讨论】:

  • 您好 Alex,感谢您的快速响应,它没有任何作用。我尝试添加一些代码,以查看它是否调用了该方法,例如在我的方法中添加带有puts "-----IT WORKS-----" 的行,但它不会打印它。所以它不会执行我的方法。
猜你喜欢
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 2015-08-06
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多