title: Making a Javascript Loop request in rails view
body: <p>I need to download a file from a link I am given. In order to do this I must make a get request to that link. It can have 3 states:<br>
  1. Code 200 and the download will begin once the request landed<br>
  2. Code 202 which means I must repeat the request because the file is being uploaded<br>
  3. Error code and I must create a dom element that shows that.</p>
  
  <p>How it works:<br>
  I make a request to this rails action:  </p>
  
  <pre><code>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 
  </code></pre>
  
  <p>This in turn starts the file upload. Which I don't know when it's ready(depending on how big the file it is). Now I must create a javascript get request to a link that follows the indications I have given at the beginning of the post. And integrate it in the by_month.html.erb view from rails . The javascript I managed to write is:</p>
  
  <pre><code>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);
      }
  </code></pre>
  
  <p>However I don't think it works. Any ideas of how I can do this?(redirect_to_main and make_error_css are functions that I will implement myself later).</p>