2014-01-29 23 views
0

嗨,我試圖調用後面的代碼使用jQuery的代碼。但是該方法不應該返回任何值。我創建了Web方法並調用它。我的方法是這樣的調用代碼背後的方法使用jQuery的void返回類型

$.ajax({ 
       type: "post", 
       contentType: "application/json;charset=utf-8", 
       dataType: "json", 
       url: "Model/Services/Information.asmx/TestingExport", 
       data: JSON.stringify(empno), 
       success: function (resp) { 
        if (resp.ok) { 
         $('#ExportEmpInfo').empty(); 
         $('#ExportEmpInfo').append('<p><b>' + 'Data Has been Exported to Excel successfully ' + '</b></p>'); 
        } 
       else { 

        $('#ExportEmpInfo').empty(); 
        $('#ExportEmpInfo').append('<p><b>' + 'Exporting to Excel Failed' + '</b></p>'); 

       } 


      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       alert('TextStatus:' + textStatus + ' errorThrown: ' + errorThrown); 
      } 
     }) 

但是測試Export方法將調用其他方法將最後調用將數據導出到Excel格式的方法。在最後的方法有其用於數據導出至Excel

response.Clear(); 
      response.ClearContent(); 
      response.Charset = ""; 
      response.ContentType = "application/vnd.ms-excel"; 

我想這是具有衝突作爲JSON中提到的reponse.ContentType的並且被投擲「ParseError對象錯誤」的代碼部分。這是拋出錯誤。請幫助我如何調用一個不返回任何值的函數。如果返回類型是必要的話,我可以返回任何值,但如何處理response.ContentType

拋出的錯誤

我導出到Excel的功能是

public static void ExportToExcel(HttpResponse response, DataSet ds) 
     { 
      try 
      { 
       response.Clear(); 
       response.ClearContent(); 
       response.Charset = ""; 
       response.ContentType = "application/vnd.ms-excel"; 

       DataGrid dg = new DataGrid(); 

       for (int i = 0; i < ds.Tables.Count; i++) 
       { 

        StringWriter stringWriter = new StringWriter(); 

        HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(stringWriter); 

        stringWriter = new System.IO.StringWriter(); 
        htmlWriter = new System.Web.UI.HtmlTextWriter(stringWriter); 
        dg.DataSource = ds.Tables[i]; 
        dg.DataBind(); 
        dg.RenderControl(htmlWriter); 
        response.Write(stringWriter.ToString()); 
       } 
       response.Flush(); 
       response.Close(); 
       response.Write(""); 

      } 

控制是通過這些代碼會與任何錯誤,但它不會生成Excel表單,因爲它是通過普通代碼後面的方法調用而不是從Web方法調用時生成的。

+0

如果您不需要返回任何數據,那麼爲什麼首先要處理響應? –

+0

這些響應語句在從ajax調用你的方法時什麼也不做(除了導致你的問題) –

+0

我從腳本調用的函數不會返回任何值,但它會調用其他函數,它會調用一個函數excel文件,因此響應代碼是用於生成excel文件的代碼。 – user2864496

回答

2

正確的解決方案應該是刪除設置響應的代碼,如果沒有預期的響應。但是,如果這是不可能的(我承擔的「最後一節課」未使用在這裏),那麼這行

dataType: "json" 

告訴jQuery的,它應該會在JSON格式的響應。然而,它正在接受一些不同的東西,並沒有解析它,拋出錯誤。只要刪除這一行。

+0

Hai Andrei我刪除了dataType:「json」現在不會拋出任何錯誤,但它不會生成excel文件。它正在通過生成excel文件的功能,但它沒有顯示任何提示,通常顯示的選項打開保存取消Excel表單。我的出口到excel函數在上面提到的問題 – user2864496

+0

@ user2864496這聽起來像是一個單獨的問題。我會開始一個新問題,並清楚解釋你的新問題以及你採取的調查步驟。 – mason

+0

@ msm8bball謝謝 – user2864496

0

我認爲TestingExport方法的返回類型爲void。然後不需要保持成功:function(resp){if(resp.ok)....你可以刪除resp作爲參數並檢查它是否也是如此。如果你想保留它,而不是檢查resp.ok,請檢查!(resp === null)。

相關問題