我正在开发一个我没有开始的项目,我有一个输入功能就是将数据保存到数据库。输入留给此函数(SaveData),它不起作用。我真的不太了解ajax和json,所以有人可以看看这有什么不对吗?
功能如下:
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);
}
});
SaveConfiguration方法是这样的:
[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 "";
}
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();
}
}
}
再次感谢! =)
答案 0 :(得分:0)
据我所知,假设URL有效且其余的html,看起来你的数据属性不正确。你有:
data: "{ 'jsonData': [" + JSON.stringify(columns) + "] }",
根据我的经验,你应该在整篇文章中使用stringify:
data: JSON.stringify({jsonData: columns}),
答案 1 :(得分:0)
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);
}
});