我试图从前端JavaScript发送对象数组到asp.net Web表单中的方法,错误:https://localhost:44396/Input.aspx/UpdateData

7gyucuyw  于 2023-11-20  发布在  .NET
关注(0)|答案(2)|浏览(151)

我有三个输入,app name,from date和to date。我将它们添加到一个创建对象数组的表中。然后尝试使用apache将此数组发送到web表单中的aspx.cs文件中的方法。但我得到了错误。
JavaScript代码:

document.addEventListener("DOMContentLoaded", function()
  {
  console.log("DOMContentLoaded event fired");
  var updateBtnClick = document.getElementById("<%= updateBtn.ClientID %>");
  //"ContentPlaceHolder1_updateBtn" 
  if (updateBtnClick) 
    {
    console.log("Update button element found");
    updateBtnClick.addEventListener('click', function(e) 
      {
      e.preventDefault();
      console.log("Update button is clicked");
      var jsonData = JSON.stringify(tableData);
      console.log(jsonData);
      $.ajax(
        { url         : "Input.aspx/UpdateData"
        , type        : "POST"
        , data        : jsonData
        , contentType : "application/json"
        , success     : function(response) 
          {
          console.log("Data sent to the backend successfully");
          }
        , error       : function(error) 
          {
          console.error("Error sending data to the backend: "+JSON.stringify(error));
          }
        });
      });
    } 
  else 
    {
    console.log("Update button element not found");
    }
  });

字符串
aspx.cs:

[WebMethod] public static string UpdateData(List<Downtime> jsonData)
  {
  if (jsonData != null && jsonData.Count > 0) 
    { 
    string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connectionString)) 
      { 
      conn.Open(); 
      foreach (var item in jsonData) 
        { 
        string   dropdownValue = item.App_name; 
        DateTime fromDate      = item.From_date; 
        DateTime toDate        = item.To_date; 
        string   sqlQuery      = "UPDATE DowntimeApplications SET From_date = @FromDate, To_date = @ToDate where App_name = @name"; 
        using (SqlCommand command = new SqlCommand(sqlQuery, conn)) 
          { 
          command.Parameters.AddWithValue("@FromDate", fromDate); 
          command.Parameters.AddWithValue("@ToDate", toDate); 
          command.Parameters.AddWithValue("@name", dropdownValue); 
          command.ExecuteNonQuery(); 
          } 
        } 
      HttpContext.Current.Response.ContentType = "application/json"; 
      var response = new { success = true, message = "Data updated successfully" }; 
      return JsonConvert.SerializeObject(response); 
      } 
    } 
  var noDataResponse = new { success = false, message = "No data to update" }; 
  HttpContext.Current.Response.ContentType = "application/json"; 
  return JsonConvert.SerializeObject(noDataResponse); 
  }

wa7juj8i

wa7juj8i1#

[WebMethod] public static string UpdateData(List<Downtime> jsonData)

字符串
webmethod参数名为jsonData,因此需要传递带有此键的对象。
下面的代码应该可以工作:

var jsonData = {};//jsonData matches the parameter name of the webmethod
        jsonData["jsonData"] = tableData; 

        var payload = JSON.stringify(jsonData);

        $.ajax(
            {
                url: "Input.aspx/UpdateData"
                , type: "POST"
                , data: payload
                , contentType: "application/json"
                , success: function (response) {
                    console.log("Data sent to the backend successfully");
                }
                , error: function (error) {
                    console.error("Error sending data to the backend: " + JSON.stringify(error));
                }
            });

y0u0uwnf

y0u0uwnf2#

我手动将数据序列化为JSON。我直接在AJAX调用中传递tableData。

$.ajax({ url: "Input.aspx/UpdateData", type: "POST", /*data: payload,*/ data: JSON.stringify({ jsonData: tableData }), contentType: "application/json", success: function (response) { console.log("Data sent to the backend successfully"); }, error: function (error) { console.error("Error sending data to the backend: " + JSON.stringify(error)); } });

字符串

相关问题