Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在rails视图中发出Javascript循环请求_Javascript_Jquery_Ruby On Rails_Ruby_Coffeescript - Fatal编程技术网

在rails视图中发出Javascript循环请求

在rails视图中发出Javascript循环请求,javascript,jquery,ruby-on-rails,ruby,coffeescript,Javascript,Jquery,Ruby On Rails,Ruby,Coffeescript,我需要从给我的链接下载一个文件。为了做到这一点,我必须向该链接发出get请求。它可以有3种状态: 1.代码200,请求到达后将开始下载 2.代码202,这意味着我必须重复请求,因为文件正在上载 3.错误代码,我必须创建一个显示该错误的dom元素 工作原理: 我请求执行此rails操作: def by_month export_form = Commissions::ByMonthForm.new(current_user) if export_form.submit(params

我需要从给我的链接下载一个文件。为了做到这一点,我必须向该链接发出get请求。它可以有3种状态:
1.代码200,请求到达后将开始下载
2.代码202,这意味着我必须重复请求,因为文件正在上载
3.错误代码,我必须创建一个显示该错误的dom元素

工作原理:
我请求执行此rails操作:

def by_month
    export_form = Commissions::ByMonthForm.new(current_user)
    if export_form.submit(params)
      @export = export_form.export
    else
      show_errors export_form.errors
    end
  end 
这将依次启动文件上载。我不知道它什么时候准备好(取决于文件的大小)。现在,我必须创建一个javascript get请求,该请求指向一个链接,该链接遵循我在本文开头给出的指示。并将其集成到rails的by_month.html.erb视图中。我设法编写的javascript是:

function httpGetAsync(theUrl){
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 4) {
          if (xmlHttp.status == 200) {
            redirect_to_main();
          }
          else if(xmlHttp.status == 202) {
            httpGetAsync(theUrl);   
          }
          else {
            make_error_css();
          }
        }
      }
      xmlHttp.open("GET", theUrl, true); // true for asynchronous
      xmlHttp.send(null);
    }

然而,我认为它不起作用。有什么想法吗?(重定向到\u main和make\u error\u css是我稍后将自己实现的函数)。

根据下面的评论更新

你能试试这个吗

function httpGetAsync(theUrl){
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 4) {
          if (xmlHttp.status == 200) {
            redirect_to_main();
          }
          else if(xmlHttp.status == 202) {
           setTimeout(
            makeRequest(theUrl),
             3000);   
          }
          else {
            make_error_css();
          }
        }
      }
      //makeRequest(xmlHttp, theUrl);
      xmlHttp.open("GET", theUrl, true); // true for asynchronous
      xmlHttp.send(null);

    }

function makeRequest(theUrl){
           httpGetAsync(theUrl);
        }

makeRequest()
是状态为202时再次发出请求的地方。

但如果状态为202,我需要重新生成它(由于我正在等待文档下载,因此可能有数量不确定的202个答案,我需要每3秒或其他时间重复此调用。因此,据我所知,如果状态为202(不管有多少次),每次你都要等待3秒钟,然后再次进行新的调用?我的理解正确吗?函数makeRequest(theUrl){setTimeout(httpGetAsync(theUrl),3000);};类似这样的内容:Dhi@LucianTarna我做了一些更改,看看这是否回答了你的问题:)