首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax PageMethods未被调用

Ajax PageMethods未被调用
EN

Stack Overflow用户
提问于 2014-08-09 14:38:41
回答 2查看 50关注 0票数 0

我让下面的javascript在asp.net中调用webmethd

代码语言:javascript
复制
 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是这样的:

代码语言:javascript
复制
        [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。

问题出在哪里?请帮帮忙

谢谢

EN

回答 2

Stack Overflow用户

发布于 2014-08-09 14:46:34

您必须对该方法进行post。我不确定post之后的操作是否依赖于操作的结果,但如果是这样的话,您必须将它放在//do something here所在的$.post中。

代码语言:javascript
复制
   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;}
        }
票数 0
EN

Stack Overflow用户

发布于 2014-10-28 23:41:42

我找到了解决方案。

我使用的是url重写。当我检查我的代码时,我发现了

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25215945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档