简体   繁体   中英

Saving data with ajax and json

I am working on a project that I didn't start and I have an input that is function is to save the data to the data base. The input leaves to this function (SaveData) and it isn't working. I really don't understand much from ajax and json, so could anyone please see what's wrong with this?

The function is this:

 function SaveData() {
     var columns = [];

     $("#FeaturedContent_m_visible_cols li").each(function (i, elem) {
        columns[i] = $(elem).attr("colid");
     });

     $.ajax({
        async: false,
        type: "POST",
        url: "ConfigList.aspx/SaveConfiguration",
        data: "{ 'jsonData': [" + JSON.stringify(columns) + "] }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
        },
        error: function (response) {
           alert(jQuery.parseJSON(response.responseText).Message);
        }
     });

The SaveConfiguration method is this:

[System.Web.Services.WebMethod]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  public static string SaveConfiguration(dynamic jsonData)
{
 Object[] column = (Object[])jsonData[0];
  string   col_id;
  int      i;

  for (i=0; i<column.Length; i++) {
     col_id = (string)column[i];
  }

  SaveTableConfiguration();

  return "";
  }

And the SaveTableConfiguration:

 public void SaveTableConfiguration(int list_id, List<TableHeader> lista)
  {
    int           i;
    TableHeader   row;
    SetListConfig set_lconfig = new SetListConfig(m_connection, m_language);

set_lconfig.LoadRecordset("LC_LISTID=" + list_id.ToString(),    m_transaction);
     while (!set_lconfig.IsEOF()) {
        set_lconfig.m_LC_EDITABLE = 0;
        set_lconfig.Update();

        set_lconfig.MoveNext();
     }

     for (i=0; i<lista.Count;i++) {
        row=lista[i];

set_lconfig.LoadRecordset("LC_LISTID=" + list_id.ToString() + " AND                LC_COLTBL='" + row.header_col + "'", m_transaction);
        if (set_lconfig.IsEOF()) {
           set_lconfig.m_LC_EDITABLE = 1;
           set_lconfig.m_LC_ORDER = i+1;
           set_lconfig.Update();
        }
     }
  }

Thanks Once more! =)

From what I can tell, assuming the URL is valid and the rest of your html, it looks like your data property is incorrect. You have:

data: "{ 'jsonData': [" + JSON.stringify(columns) + "] }",

And in my experience, you should use stringify on the entire piece:

data: JSON.stringify({jsonData: columns}),
function SaveData() {
 var columns = [];

 $("#FeaturedContent_m_visible_cols li").each(function (i, elem) {
    columns[i] = $(elem).attr("colid");
 });
var datum={'jsonData':columns};

 $.ajax({
    async: false,
    type: "POST",
    url: "ConfigList.aspx/SaveConfiguration",
    data: JSON.stringify(datum),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
    },
    error: function (response) {
       alert(jQuery.parseJSON(response.responseText).Message);
    }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM