【发布时间】:2016-11-13 07:44:09
【问题描述】:
我想使用 jquery json 为multipledelete 编写代码。
这是jquery代码:
function DeleteSelected() {
var categories = new Array();
debugger;
// iterate all checkboxes and obtain their checked values, unchecked values are not pushed into array
$("input[type='checkbox']").each(function ()
//$('input[type=checkbox]').prop("checked", this.checked)
{
this.checked ? categories.push($(this).val()) : null;
});
// assume urldata is your web method to delete multiple records
var urldata = "WebForm5.aspx/deleteRecord";
debugger;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: urldata,
data: "{ 'Id':'"+JSON.stringify( categories)+"' }", // used to convert array into proper JSON format
dataType: "json",
success: function (dt) {
// done whatever tasks if succeeded
alert("entry deleted");
debugger;
$("#example1").DataTable();
//$("#example1").bind;
debugger;
},
error: function (result) {
alert("Error");
}
});
}
这是 aspx 代码(用于多次删除的代码隐藏):
[WebMethod]
// note clear difference between single and multiple selections
public static void deleteRecord(List<int> Id)
{
clsCategoryBL objproject = new clsCategoryBL();
// iterate through input list and pass to process method
for (int i = 0; i < Id.Count; i++)
{
objproject.CategoryDelete(Id[i]);
}
}
我想将 id 传递给 aspx 页面,但问题是输出出现错误弹出窗口。
error: function (result) {
alert("Error");
}
【问题讨论】:
-
尝试将
alert("Error")替换为alert(result)以查看错误所在。 -
错误是[object][Object]
-
使用
console.log()而不是alert() -
我想将 id 传递给我的 aspx 页面,但它 terurns null 显示它会返回 false,所以你能告诉我如何传递 id 吗?如果我使用 console.log() 没有弹出窗口..那么如何解决?
-
看起来像语法错误尝试用
'{"ID":"' + JSON.stringify( categories)+ '"}';替换"{ 'Id':'"+JSON.stringify( categories)+"' }"
标签: c# jquery html asp.net json