c# - 尝试使用 AJAX 从 c# 调用方法时出现 ArgumentException
问题描述
我需要用 AJAX 将一个方法调用到前端,我用 $.ajax() 做了,但现在它抛出了一个 ArgumentException,说该方法是未知的。知道如何解决这个问题吗?
我发现的许多答案都说我需要将其公开和静态。我这样做了。该方法也不需要参数。
从我可以从堆栈跟踪中了解到,它可能需要两个参数?Object sender 和 EventArgs e,只是当我添加这些时,我不知道从 AJAX 发送什么。这两个参数是什么,问题可能存在吗?
[WebMethod]
public static void ExportToExcelTemp()
{
object[][] table = HttpContext.Current.Session["ExcelData"] as object[][];
DataTable dttable = new DataTable();
for (int i = 0; i < table[0].Length; i++)
{
dttable.Columns.Add(i.ToString());
}
foreach (object[] data in table)
{
dttable.Rows.Add(data);
}
String fileName = (String)HttpContext.Current.Session["uploadFilename"];
string _fileName = Path.GetFileNameWithoutExtension(fileName);
using (ClosedXML.Excel.XLWorkbook wb = new ClosedXML.Excel.XLWorkbook())
{
var ws = wb.Worksheets.Add((fileName.Length > 31 ? fileName.Substring(0, 31) : fileName));
ws.FirstRow().FirstCell().InsertData(dttable.Rows);
ws.Columns().AdjustToContents();
// ws.Tables.First().ShowHeaderRow = false;
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.Charset = "";
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + _fileName + ".xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(System.Web.HttpContext.Current.Response.OutputStream);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}
}
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
traditional: true,
//async: false,
data: {},
url: "PdfViewer.aspx/ExportToExcelTemp",
success: function (data) {
DISMISS_LOADING();
showSuccess(data)
if (data.status == 401) {
NO_AUTHENTICATION();
}
return data.success;
},
error: function (xhr, textStatus, err) {
showError("readyState: " + xhr.readyState + "\n responseText: " + xhr.responseText + "\n status: " + xhr.status
+ "\n text status: " + textStatus + "\n error: " + err);
},
failure: function () {
DISMISS_LOADING();
showError("Testing Failure");
return false;
}
});
stacktrace:
[ArgumentException: Unknown web method ExportToExcelTemp.
Parameter name: methodName]
System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +621102
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +207
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +71
解决方案
推荐阅读
- c++ - 带有双括号的 C++ if 语句有什么作用?
- javascript - 为什么在类方法中使用 ES6 的 Proxy 不起作用?
- css - WOFF webfont 生成器生成大量垃圾(可能是原始字体)数据
- vba - 输出哪些 Excel 文件受密码保护
- php - 将数据颤动应用程序发布到 postgresql db php
- swift - 有没有办法检查信息面板当前是否在 AVPlayerViewController 中切换?
- certificate - 一个 rfc5280 证书中可以有多个相同的 CHOICE 字段吗?
- python-3.x - 如何删除在 python3 字符串对象中显示为 `\uxxx` 的特殊字符?
- python - 仅在选择列的 Pandas 数据框中添加一行
- c# - Arkit Scene (Xamarin) 中的 PanGesture 和对象翻译问题