嗨,我試圖調用後面的代碼使用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方法調用時生成的。
如果您不需要返回任何數據,那麼爲什麼首先要處理響應? –
這些響應語句在從ajax調用你的方法時什麼也不做(除了導致你的問題) –
我從腳本調用的函數不會返回任何值,但它會調用其他函數,它會調用一個函數excel文件,因此響應代碼是用於生成excel文件的代碼。 – user2864496