0

I need to call a method with AJAX into front-end and I did with $.ajax() but now it throws an ArgumentException that says the method is unknown. Any idea how to fix this?

Many of the answers that I found said that I need to make it public and static. I did that. Also the method needs no parameters.

From what I can understand from the stacktrace, it maybe needs two parameters? Object sender and EventArgs e, it's just that when I add these, I don't know what to send from AJAX. What are these two parameters and can the problem be there?

        [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
8kk
  • 140
  • 9
  • It might be something outside the file, like the aspx page itself might not be properly associated with the code behind. Might be worth checking that and posting it. – Adam Wise May 03 '19 at 12:29
  • @AdamWise the codebehind is associated with the right page. Checked it. – 8kk May 03 '19 at 13:12

0 Answers0