Monitoring a server process in a Rails application using AJAX XMLHttpRequest

I use the following on a webpage but cannot get a response from the server during processing

<script type="text/javascript"> <!-- function updateProgress() { //alert('Hello'); new Ajax.Request('/fmfiles/progress_monitor', { parameters: 'authenticity_token=' + encodeURIComponent(AUTH_TOKEN), onSuccess: function(response) { alert(response.responseText); fillProgress('progressBar',response.responseText); } }); } //--> </script> <% form_for( :fmfile, :url => '/fmfiles', :html => { :method => :post, :name => 'Form_Import', :enctype => 'multipart/form-data' } ) do |f| %> ... <%= f.file_field :document, :accept => 'text/xml', :name => 'fmfile_document' %> <%= submit_tag 'Import', :onClick => "setInterval('updateProgress()', 2000);" %> 

The "create" method in fmfiles_controller.rb then successfully processes the file and receives the correct results (according to the submit button on the form). If I uncomment the line '// alert (' Hello '), I get a dialog saying Hello every 2 seconds ... as expected.

However, the server never registers a call to the progress_monitor method in the “files” of a failed attempt.

If I click the link

 <a href="#" onclick="updateProgress();">Run</a> 

it makes a call to the server, receives a response and displays a dialog, so I assume that the routes, syntax and naming are all right.

I really don't know why this is not working. Is it because 2 methods in one controller are called via URLs?

I am using Rails 2.1.0 in a development environment on OS X 10.5.5 and using Safari 3.1.2

(NB This follows from another question , but I think it is quite different from my own question.)

0
source share
1 answer

If you do not see the message in your log file to call progress_monitor, the request may never be sent.

Try the following:

  • Try using the full URL instead of the relative URL for Ajax.Request. I had problems with relative URLs in some browsers with Ajax.Request.

  • Enable Firebug or IE Developer Toolbar. You should be able to see if the progress_monitor call works or not. If there is a java script error, you will see an error using these tools.

+2
source

Source: https://habr.com/ru/post/1277063/


All Articles