我有三个输入,应用程序名称,从日期和日期.我把它们添加到一个创建对象数组的表中.然后try 在web表单中使用aplog将此数组发送到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); 
  }

推荐答案

我手动将数据序列化为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)); } });

Javascript相关问答推荐

如何在Jest中调用setupFile下列出的文件所需的函数?

原型链中的同一财产怎么会有不同的价值?

JavaScript:循环访问不断变化的数组中的元素

Mongodb拥有5亿个文档,我想根据JavaScript驱动程序中的两个字段使用regEx进行搜索,而不是模式

Bootstrap动态选项卡在切换选项卡后保持活动状态,导致元素堆叠

传递一个大对象以在Express布局中呈现

从mat—country—select获取整个Country数组

为什么ngModel不能在最后一个版本的Angular 17上工作?'

如何从URL获取令牌?

在react JS中映射数组对象的嵌套数据

VUE 3捕获错误并呈现另一个组件

使用VUE和SCSS的数字滚动动画(&;内容生成)

在使用REACT更改了CSS类之后,无法更改CSS样式

Reaction-SWR-无更新组件

如何防止ionic 输入中的特殊字符.?

OnClick更改Json数组JSX中的图像源

React:防止useContext重新渲染整个应用程序或在组件之间共享数据而不重新渲染所有组件

我不知道如何纠正这一点.

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

无法使用npm install安装react-dom、react和next