这是我的javascript代码:
function doTheDelete(doIDeleteExpenses) {
if (selectedExpensesList.length > 0) {
$.ajax({
type: "POST",
//url: "/Tasks/ViewTasks.aspx/deleteTasksAndLinkedItems",
url: '<%=ResolveUrl("~/Expenses/ViewExpenses.aspx/deleteSelectedExpense")%>',
data: "{'DeleteExpenses' : " + "'" + doIDeleteExpenses + " '}",
//dataaaaaa
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var ss = data.d;
if (ss.length > 0) {
for (var i = 0; i < ss.length; ++i) {
$.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true });
alert(ss[i]);
}
}
$("#viewTasksGrid").flexReload();
},
error: function (data) {
$.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true, modal: true });
alert('Error Deleting Expense');
if (window.console) {
console.log(data);
}
}
});
} else {
showMessage('No expenses are selected.');
}
}
function getSelectedExpenseIDs() {
var selectedExpensesList = new Array;
var i = 0;
$('.expenseCheckBox:checked').each(function () {
if ($(this)[0].id !== "checkAllExpenses") {
selectedExpensesList[i] = $(this)[0].id.split('_')[1];
++i;
}
});
return selectedExpensesList;
}
这是我在aspx.cs中永远不会被调用的方法:
[WebMethod]
public static string[] deleteSelectedExpense(bool DeleteExpenses, String[] ExpID)
{
var rList = new List<string>();
//var canDeleteTasks = false;
//var canDeleteTrackers = false;
var canDeleteExpenses = false;
var investigatorID = (int)HttpContext.Current.Session["InvestigatorID"];
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
var cmd = new SqlCommand("p_Admin_Permissions_CanDeleteExpenses", conn);
cmd.Parameters.Add(new SqlParameter("@InvestigatorID", SqlDbType.Int));
cmd.Parameters["@InvestigatorID"].Value = investigatorID;
cmd.CommandType = CommandType.StoredProcedure;
try
{
conn.Open();
canDeleteExpenses = (bool)cmd.ExecuteScalar();
}
catch (SqlException sql)
{
if (!rList.Contains("Can not connect to the database. Please try again."))
rList.Add("Can not connect to the database. Please try again.");
}
catch (Exception ex)
{
if (!rList.Contains("An Error Occured"))
rList.Add("An Error Occured");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
if (canDeleteExpenses)
{
foreach (var expense in ExpID)
{
if (canDeleteExpenses && DeleteExpenses)
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString());
cmd = new SqlCommand("p_DeleteExpenses", conn);
cmd.Parameters.Add(new SqlParameter("@TaskID", SqlDbType.Int));
cmd.Parameters["@ExpID"].Value = int.Parse(expense);
cmd.Parameters.Add("@Message", SqlDbType.NVarChar, 50);
cmd.Parameters["@Message"].Direction = ParameterDirection.Output;
cmd.CommandType = CommandType.StoredProcedure;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException sql)
{
if (!rList.Contains("Error Connecting to the Database. Unable To Delete Expense(s)."))
rList.Add("Error Connecting to the Database. Unable To Delete Expense(s).");
}
catch (Exception ex)
{
if (!rList.Contains("An Error Occured"))
rList.Add("An Error Occured");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
else if (!canDeleteExpenses && DeleteExpenses)
{
rList.Add("You do not have permission to delete Expenses");
}
else
{
if (!rList.Contains("You do not have permission to delete the task(s)."))
rList.Add("You do not have permission to delete the task(s).");
}
}
}
//var serializer = new JavaScriptSerializer();
//var re = serializer.Serialize(rList.ToArray());
return rList.ToArray();
}
当我在浏览器控制台中时,它告诉我一个内部错误(500)。好吧,显而易见的原因。
答案 0 :(得分:1)
你的参数错了。 deleteSelectedExpense
方法采用bool
和string[]
参数,但您只是将bool传递给标记,以删除它们。
expId : [ 1, 2, 3 ]
,这就是我使用myArray.join
方法并将括号放在值周围的原因。
var myArray = ['100', '200'];
$.ajax({
type: 'POST',
url: 'WebForm1.aspx/testMethod',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: '{ del: true, expId : [' + myArray.join(",") + '] }',
success: success,
error: error
});
function success(data) {
$('#result').html(data.d);
}
function error(data) {
$('#result').html(data.d);
}
的WebMethod:
[WebMethod]
public static string testMethod(bool del, string[] expId)
{
var rand = new Random();
return rand.Next().ToString();
}