![](/img/trans.png)
[英]How to make an AJAX call to different domains in Ruby on Rails 3.0
[英]Ruby on rails - How to make controller execute 2 actions simultaneously with ajax?
我在開發環境中使用ajax為同一個控制器調用兩個不同的操作。 在一個操作中,我正在一個非常緩慢的過程中更新數據庫。 在另一個動作上,我想讓百分比結束更新進度條。 我可以看到兩者都在執行,問題是第二個動作正在等待第一個動作完成自己執行,它應該每秒執行一次以獲得實際的百分比。 rails可以並行執行兩個動作嗎? 還是Webrick問題?
我按照建議嘗試了puma,但它仍在等待第一個動作,繼承了ajax代碼調用
$(document).ready(function() {
$(':button').click(function(){
var formData = new FormData($('form')[0]);
$.ajax({
url: '/quotes/upload', //Server script to process data
type: 'POST',
//Ajax events
beforeSend: progress,
success: completeHandler,
error: function(ts) { alert(ts.responseText) },
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
});
});
function progress() {
$.ajax({
url: '/quotes/status.json',
dataType: "JSON"
}).success(function (percentual) {
if (percentual >= '95') {
location.reload();
} else {
var $bar = $('.bar');
$bar.width(percentual+"%");
setInterval(progress,800);
}
});
}
這是控制台打印輸出:
Started GET "/quotes/status.json" for 127.0.0.1 at 2014-09-21 10:23:38 -0300
Processing by QuotesController#status as JSON
percentual => 0 Completed 200 OK in 4ms (Views: 0.6ms | ActiveRecord: 0.0ms)
Started POST "/quotes/upload" for 127.0.0.1 at 2014-09-21 10:23:38 -0300
Processing by QuotesController#upload as */*
Parameters: {"quotes"=>#<ActionDispatch::Http::UploadedFile:0x00000001b980b0 @tempfile=#<Tempfile:/tmp/RackMultipart20140921-5849-otvxky>, @original_filename="HIST_XXX.TXT", @content_type="text/plain", @headers="Content-Disposition: form-data; name=\"quotes\"; filename=\"HIST_XXX.TXT\"\r\nContent-Type: text/plain\r\n">}
Rendered quotes/index.html.erb within layouts/application (0.7ms)
Completed 200 OK in 10456ms (Views: 170.3ms | ActiveRecord: 0.0ms)
Started GET "/quotes/status.json" for 127.0.0.1 at 2014-09-21 10:23:49 -0300
Processing by QuotesController#status as JSON
percentual => 100 Completed 200 OK in 3ms (Views: 0.4ms | ActiveRecord: 0.0ms)
正如我在控制台上看到的那樣,它只是在上傳之前進行第一次通話,然后進行上傳,然后在完成第二次通話之后
你是對的 - Webrick一次只能處理一個請求。 也許給Thin , Puma或Unicorn一個 - 他們都處理並發請求。
注意 : 請注意,使用SQLite可能會出現類似的並發問題(特別是如果涉及寫入!)切換到PostgeSQL或MySQL。
要使用Thin :
的Gemfile
gem 'thin'
當然...
bundle install
然后啟動服務器
thin start
使用Puma :
的Gemfile
gem 'puma'
當然...
bundle install
啟動服務器
rails s puma
要使用Unicorn :
的Gemfile
gem 'unicorn'
當然...
bundle install
啟動服務器
unicorn_rails
感謝您的提示,我找到了一個解決方案:將它放在您的development.rb文件中:
config.middleware.delete Rack::Lock
你還必須改變Webrick。 它與Puma一起工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.