[英]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.
输入留给此函数(SaveData),它不起作用。 I really don't understand much from ajax and json, so could anyone please see what's wrong with this?
我对ajax和json真的了解不多,所以有人可以看看这有什么问题吗?
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: 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 "";
}
And the SaveTableConfiguration: 和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. 据我所知,假设该URL是有效的,而您的html的其余部分似乎是您的data属性不正确。 You have:
你有:
data: "{ 'jsonData': [" + JSON.stringify(columns) + "] }",
And in my experience, you should use stringify on the entire piece: 以我的经验,您应该在整篇文章中使用stringify:
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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.