页面方法的失败jQuery调用 [英] failed jquery call of page method
问题描述
我正在尝试使用jquery调用页面方法将数据从HTML表保存到数据库. 但是,jquery调用失败,并显示:500内部服务器错误. 有什么建议吗?
I'm trying to save data from HTML table to database using jquery to call a page method. However, jquery call fails with : 500 internal server error.. Any advice?
下面是代码:
脚本:
$(document).ready(function() {
$("#save").click(function(event) {
var i = 0;
var inputs = new Array();
$("#tbNames").find("input").each(function() {
inputs[i] = [this.id, this.value];
i++;
});
var columnsCount = 2;
$.ajax({
type: "POST",
url: "Default2.aspx/Save",
contentType: "application/json; charset=utf-8",
data: "{inputs: inputs, columnsCount: columnsCount}",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
});
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
});
</script>
HTML:
姓名
任务
删除
Name
Task
Delete
<input id="save" type="button" value="Save data" />
和分页方法的C#代码:
and C# code of page method:
[WebMethod ()]
public static List<Hashtable> ParseJson(string[] array, int rowsCount)
{
var result = new List<Hashtable>();
List<string[]> list = new List<string[]>();
foreach (var item in array)
{
int comma = item.IndexOf(',');
list.Add(new string[] { item.Substring(0, comma), item.Substring(comma + 1) });
}
int i = 0;
while (i < list.Count)
{
var dict = new Hashtable();
int j = i + rowsCount;
for (; i < j; i++)
{
dict.Add(list[i][0], list[i][1]);
}
result.Add(dict);
}
return result;
}
[WebMethod()]
public static string Save(string[] inputs, int columnsCount)
{
List<Hashtable> r = ParseJson(inputs, columnsCount);
List<TestClass> listOfTasks = new List<TestClass>();
foreach (var item in r)
{
TestClass tc = new TestClass();
tc.name = (string)item["name"];
tc.task = (string)item["task"];
listOfTasks.Add(tc);
}
DBManager dbm = new DBManager();
string actionResult = dbm.SaveData(listOfTasks);
if (actionResult == String.Empty)
{
return "Saved!";
}
else
{
return actionResult;
}
}
推荐答案
密钥是json2.js及其stringify函数.实现该功能后,AJAX调用 看起来像这样:
The key is json2.js and its stringify function. After implementing of that function AJAX call looks like this:
$.ajax({ type: "POST",
url: "Default2.aspx/Save",
contentType: "application/json; charset=utf-8",
data: "{'inputs':" + JSON.stringify(inputs) + ",'columnsCount':1 }",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
源.
这篇关于页面方法的失败jQuery调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!