我让下面的javascript在asp.net中调用webmethd
function passpd(elem) {
if (confirm("You sure you want to delete") == true) {
var exID = $(elem).closest('table').attr('id');
exID = exID.replace("_fawad", '');
alert(exID + "abc");
PageMethods.DeleteInfo1("fawad", exID, "OnSuccessD", "OnErrorD");
alert("2");
var d1= $(elem).closest('table').attr('id');
$("#" + d1).remove();
return false;
}
else { alert("cancel deletion");return false;}
}我的webmethod是这样的:
[WebMethod]
public static string DeleteInfo1(string pname, string id)
{
string constr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ProfileOne.mdf;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(constr);
string sql = "delete from Experience where ProfileName=@p and ExperienceID=@e";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@p", SqlDbType.VarChar).Value = pname;
cmd.Parameters.Add("@e", SqlDbType.Int).Value = Convert.ToInt32(id);
con.Open();
int ret = cmd.ExecuteNonQuery();
if (ret > 0) { return "ok"; }
else { return "nok"; }
}问题是webmthod没有被调用。调用了passpd()函数中的第一个警报,但没有执行PageMthods.DeleteInfo1。
问题出在哪里?请帮帮忙
谢谢
发布于 2014-08-09 14:46:34
您必须对该方法进行post。我不确定post之后的操作是否依赖于操作的结果,但如果是这样的话,您必须将它放在//do something here所在的$.post中。
function passpd(elem) {
if (confirm("You sure you want to delete") == true) {
var exID = $(elem).closest('table').attr('id');
exID = exID.replace("_fawad", '');
alert(exID + "abc");
$.post('/PageMethods/DeleteInfo1/', { pname: 'fawad', id: exID }, function() {
//do something here?
});
alert("2");
var d1= $(elem).closest('table').attr('id');
$("#" + d1).remove();
return false;
}
else { alert("cancel deletion");return false;}
}发布于 2014-10-28 23:41:42
我找到了解决方案。
我使用的是url重写。当我检查我的代码时,我发现了
https://stackoverflow.com/questions/25215945
复制相似问题