1

我正在尝试导出包含一些数据的 csv 文件。在从视图表(jqGrid)中选择一些行并将其导出为 csv 格式后,我正在进行 ajax 调用。问题是在成功调用 ajax 后,过滤后的数据显示为警告消息而不是下载选项?有任何想法吗??

查看代码:

 function x() {

 var obj = jQuery("input:checked").map(function() { return jQuery(this).parents('tr').attr('id'); });

 var arr = jQuery.makeArray(obj);
 var data = arr.join(',');


 $.ajax({
     url : '<%= url_for :action => "export_to_csv" %>',
     type : 'POST',
     data : {data:data},
     dataType : 'json',
         success : function(response) {
           alert("Details saved successfully!!!");
         },
         error : function (request, status, error) {
                alert(request.responseText);
            }       
    });     


      }

控制器代码:

def export_to_csv
 require 'fastercsv'

 data = params['data'].split(',')
 @gos = GoSe.find(:all, :conditions => [ "id IN (?)", data])
 csv = FasterCSV.generate do |line|
 cols = ["ID","Details","PartnerCode","Sece","Date","Partner","Person","TaxName","TaxID"]
 line << cols
 @gos.each do |entry|                
 line << [entry.id, entry.description, entry.code, entry.ellipsis, entry.oDate, entry.name, entry.people_name, entry.tax_name, entry.tax_id ]
     end  
  end

send_data(csv, 
:type => 'text/csv; charset=iso-8859-1; header=present', 
:disposition => "attachment; filename=go_data_#{Time.now.strftime('%d-%m-%y--%H-%M')}.csv") 

end
4

1 回答 1

1

随着代码中的以下更新..它的工作..试试看!

function x() {

var obj = jQuery("input:checked").map(function() { return jQuery(this).parents('tr').attr('id'); });

var myAjaxVariable = null;
var arr = jQuery.makeArray(obj);
var data = arr.join(',');


$.ajax({
 url : '<%= url_for :action => "export_to_csv" %>',
 type : 'POST',
 data : {data:data},
 dataType : 'json',
 async : false,
     success : function(response) {myAjaxVariable = response;},
     error : function (request, status, error) {
            alert(request.responseText);
        }       
  });   

window.open( "data:text/csv;charset=utf-8," + escape(myAjaxVariable))  


}
于 2012-10-08T09:20:25.763 回答