首页 > 解决方案 > 尝试使用 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#ajaxvisual-studiovisual-web-developer-2010

解决方案


推荐阅读