2013-04-17 31 views
0

我現在有這個標記有兩個環節,我想有通過AJAX都呼籲。

我有它運行基於使用的開始和結束日期查詢搜索,並且我有一個導出到Excel鏈接,我想有下載與使用的開始和結束日期的Excel文檔。

一切工作正常,除了獲得#export$.ajax調用下載Excel文檔。

我怎樣才能jQuery來下載的Excel文檔?

<a href="#" class="buttonH bBlue" id="export">Export to Excel</a> 
<a href="#" class="buttonH bBlue" id="search">Search</a> 
<div class="headInput" id="reportsDateSearch"> 
    <input type="text" name="start_date" placeholder="<%= @end_date %>" class="datepicker" id="endDate" /> 
    <input type="text" name="end_date" placeholder="<%= @start_date %>" class="datepicker" id="startDate" />    
</div> 

jQuery的AJAX調用:

// This call gets the data based on the start and end date used 
    $('#search').on('click', function() { 
     $.ajax({ 
      type: "GET", 
      beforeSend: function(xhr) { 
       xhr.setRequestHeader("Accept", "text/javascript"); 
      }, 
      url: '<%= reports_affirmative_action_path %>', 
      data: {start_date: $('#startDate').val(), end_date: $('#endDate').val()} 
     }, null, 'script'); 
    }); 


// This call gets the data based on the start and end date used, but i'm trying to get it to return the xlsx format. 
    $('#export').on('click', function() { 
     $.ajax({ 
       type: "GET", 
       beforeSend: function(xhr) { 
        xhr.setRequestHeader("Accept", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
       }, 
       url: '<%= reports_affirmative_action_path(:format => :xlsx) %>', 
       data: {start_date: $('#startDate').val(), end_date: $('#endDate').val()} 
     }, null, 'script'); 
    }); 
+0

你想呈現給用戶一個下載對話框?或者你是否試圖獲得用於jQuery的csv。 –

+0

下載對話框。 – Catfish

+0

通常在下載對話框中,您只需使用服務器端腳本發送適當的頭文件,包括mime-type - 可以通過jQuery方便下載對話框?我知道它可以發送請求,但實際下載是基於服務器端腳本。 – 2013-04-17 15:07:11

回答

1

最簡單的方法是隻更新點擊的錨標記的href。

$('#export').on('click', function() { 
    this.href = "<%= reports_affirmative_action_path(:format => :xlsx) %>?" + 
     $.param({start_date: $('#startDate').val(), end_date: $('#endDate').val()}); 
}); 
+0

賓果。謝啦。 – Catfish