使用AJAX XMLHttpRequest监视Rails应用程序上的服务器端进程

时间:2022-04-01 15:55:35

I'm using the following in the web page but can't get a response from the server while it's 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 happily processes the file and gets the right results (as per the submit button on the form). If I uncomment the '//alert('Hello')' line I get a dialog saying Hello every 2 seconds ... as expected.

fmfiles_controller.rb中的'create'方法然后愉快地处理文件并获得正确的结果(根据表单上的提交按钮)。如果我取消注释'// alert('Hello')'行,我会得到一个对话,说你好每2秒......正如预期的那样。

However, the server never logs any call to 'progress_monitor' method in 'files' not even a failed attempt.

但是,服务器永远不会在'files'中记录对'progress_monitor'方法的任何调用,甚至不会尝试失败。

If I click the link

如果我点击链接

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

it makes a call to the server, gets a response and displays the dialog, so I assume the routes and syntax and naming is all OK.

它调用服务器,获取响应并显示对话框,因此我假设路由,语法和命名都可以。

I really don't know why this isn't working. Is it because 2 methods in the same controller are being called via URLs?

我真的不知道为什么这不起作用。是因为同一个控制器中的2个方法是通过URL调用的吗?

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

我在OS X 10.5.5的开发环境中使用Rails 2.1.0并使用Safari 3.1.2

(N.B. This follows on from another question, but I think it's sufficiently different to merit its own question.)

(N.B.这是从另一个问题开始的,但我认为这与其自身的问题有很大不同。)

1 个解决方案

#1


2  

If you are not seeing messages in your log file for the call to 'progress_monitor' then it is possible that the request is never being sent.

如果您没有在日志文件中看到“progress_monitor”调用的消息,那么可能永远不会发送请求。

Try this:

  1. Try using the full URL instead of the relative URL for the Ajax.Request. I have had problems with relative URLs on some browsers with the Ajax.Request.

    尝试使用完整的URL而不是Ajax.Request的相对URL。我在使用Ajax.Request的某些浏览器上遇到了相对URL的问题。

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

    启用Firebug或IE Developer Toolbar。您应该能够看到对progress_monitor的调用是否有效。如果存在java脚本错误,那么您将使用这些工具清楚地看到错误。

#1


2  

If you are not seeing messages in your log file for the call to 'progress_monitor' then it is possible that the request is never being sent.

如果您没有在日志文件中看到“progress_monitor”调用的消息,那么可能永远不会发送请求。

Try this:

  1. Try using the full URL instead of the relative URL for the Ajax.Request. I have had problems with relative URLs on some browsers with the Ajax.Request.

    尝试使用完整的URL而不是Ajax.Request的相对URL。我在使用Ajax.Request的某些浏览器上遇到了相对URL的问题。

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

    启用Firebug或IE Developer Toolbar。您应该能够看到对progress_monitor的调用是否有效。如果存在java脚本错误,那么您将使用这些工具清楚地看到错误。