Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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
Javascript Rails在完成后通过Ajax发出Flash通知的方法_Javascript_Jquery_Ruby On Rails_Ajax_Flash - Fatal编程技术网

Javascript Rails在完成后通过Ajax发出Flash通知的方法

Javascript Rails在完成后通过Ajax发出Flash通知的方法,javascript,jquery,ruby-on-rails,ajax,flash,Javascript,Jquery,Ruby On Rails,Ajax,Flash,当我的控制器成功完成上传文件时,我希望通过ajax将flash通知发送回我的视图。但是,我不知道如何确切地处理来自服务器的响应。下面我所拥有的是我认为非常接近我想要的东西,但它不太起作用。任何帮助都将不胜感激。谢谢大家! 我的控制器 def import begin Thread.new do Order.import(params[:file]) ActiveRecord::Base.connection.close end

当我的控制器成功完成上传文件时,我希望通过ajax将flash通知发送回我的视图。但是,我不知道如何确切地处理来自服务器的响应。下面我所拥有的是我认为非常接近我想要的东西,但它不太起作用。任何帮助都将不胜感激。谢谢大家!

我的控制器

def import
    begin
      Thread.new do
        Order.import(params[:file])
        ActiveRecord::Base.connection.close
      end
    rescue
      redirect_to root_url, notice: "Invalid CSV file format."
    end
    format.js { flash.now[:notice] = "Here is my flash notice" }
end
我的Ajax Javascript

$(function() {

  var bar = $('.bar');
  var percent = $('.percent');
  var status = $('#status');

  $('form').ajaxForm({
    beforeSend: function() {
      status.empty();
      var percentVal = '0%';
      bar.width(percentVal);
      percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
      var percentVal = percentComplete + '%';
      bar.width(percentVal);
      percent.html(percentVal);
    },
    complete: function(xhr) {
      status.html(xhr); //I think I need to change this but not sure what to
    }
  });
});

我收到一个内部服务器错误500。OrdersController中的ArgumentError在您的代码和我的代码中导入的参数太少。如果我重定向,它会在网页中给我一个网页副本,否则会导致内部服务器错误。你知道为什么吗?
def import
    begin
      Thread.new do
        Order.import(params[:file])
        ActiveRecord::Base.connection.close
      end
    rescue
      redirect_to root_url, notice: "Invalid CSV file format."
    end
     @notice = "Here is my flash notice"
end

$(function() {

  var bar = $('.bar');
  var percent = $('.percent');
  var status = $('#status');

  $('form').ajaxForm({
    beforeSend: function() {
      status.empty();
      var percentVal = '0%';
      bar.width(percentVal);
      percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
      var percentVal = percentComplete + '%';
      bar.width(percentVal);
      percent.html(percentVal);
    },
    complete: function(response) {
       // replace the response in the div which displays notice
       status.html(response)
          }
  });
});