我在客户端有以下代码,用于从 HTML 表中检索数据,然后通过 page 方法将其发送到服务器:
function dtExportToCSV(dataTable) {
var elements = dtDataToJSON(dataTable);
var headers = dtHeadersToJSON(tableSelector);
jQuery.ajax({
type: "POST",
url: "Report.aspx/exportToCSV",
data: "{'elements': " + elements + ", 'headers': " + JSON.stringify(headers) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
if (msg.d) {
} else {
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
}
});
}
然后我在服务器端使用以下代码,将检索到的数据导出到 CSV 文件。
/// <summary>
///
/// </summary>
/// <param name="elements"></param>
/// <param name="headers"></param>
[WebMethod(EnableSession=true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static bool exportToCSV(List<object> elements, List<string> headers)
{
try
{
string attachmentType = "attachment; filename=ShortageReport.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachmentType);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
writeHeadersInfo(headers);
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
但我得到了这个 expcetion:无法评估表达式,因为代码已优化或本机框架位于调用堆栈的顶部。
有谁知道如何处理这个问题?
任何帮助将不胜感激!提前致谢!
~ 埃德奎尼诺斯