【问题标题】:Insert data with ajax jquery in asp.net在asp.net中使用ajax jquery插入数据
【发布时间】:2014-01-06 01:15:48
【问题描述】:

我不知道是什么问题..

        <script type="text/javascript">
        $(document).ready(function () {
            $("#SendCommentBtn").click(function () {
                $.ajax({

                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Pages/PageOne.aspx/SendComment",
                    data: "{'name':'" + $('#nameTxt').val() + "','comment':'" + $('#commentTxt').val() + "'}",
                    async: false,
                    success: function (response) {
                        $("#nameTxt").val("");
                        $("#commentTxt").val("");
                        alert("OK");
                    },
                    error: function () {
                        alert("ERROR");
                    }
                });
            });
        });
</script>

在我的代码后面

    [WebMethod]
    public static void SendComment(string name, string comment)
    {
        using (SqlConnection cnn = new SqlConnection("CONNECTIONSTRING"))
        {
            cnn.Open();

            int CodArt = Convert.ToInt32(HttpContext.Current.Request.QueryString["CodArt"].ToString());

            string query = @"INSERT INTO Comment VALUES(@Param1,@Param2,@Param3)";
            SqlCommand cmd = new SqlCommand(query, cnn);

            cmd.Parameters.AddWithValue("@Param1", CodArt);
            cmd.Parameters.AddWithValue("@Param2", comment);
            cmd.Parameters.AddWithValue("@Param3", name);

            cmd.ExecuteNonQuery();

        }
    }

我的问题在哪里?我找不到它。我正在使用母版页.. 是否与 Web 表单相同?此代码在母版页中。如果在我的代码后面(SendComment 方法)中放置一个断点,则网络不会停止。就像永远不会到达它一样。

【问题讨论】:

  • 您确定该网址吗? stackoverflow.com/questions/348689/…
  • @Dilish 嗨,mm 问题是我使用了母版页,而 SendComment 方法在母版页后面的代码中。我如何从 webforms 调用此方法?
  • 您不能将SendComment web 方法移动到页面“PageOne.aspx”本身吗?此外,如果您使用的是母版页,您可以使用 $('#') 其中 controID 是元素的 id,通过 jquery 获取控件。
  • 我认为它不会起作用。见这里stackoverflow.com/questions/8577539/…

标签: jquery asp.net ajax


【解决方案1】:

试试这个可能会帮助你解决你的问题

var name = $.trim($('#<%=nameTxt.ClientID %>').val());
var comment= $.trim($('#<%=nameTxt.ClientID %>').val());
$.ajax({

                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Pages/PageOne.aspx/SendComment",
                data: "{'name':'" + name + "','comment':'" + comment + "'}",
                async: false,
                success: function (response) {
                    $("#nameTxt").val("");
                    $("#commentTxt").val("");
                    alert("OK");
                },
                error: function () {
                    alert("ERROR");
                }
            });

【讨论】:

    【解决方案2】:

    我已附上示例代码供您参考..

      <script type="text/javascript">
        $(document).ready(function () {
            $("#SendCommentBtn").click(function (e) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/Pages/PageOne.aspx/SendComment",
                    data: "{'name':'" + $('#<%= nameTxt.ClientID %>').val() + "','comment':'" + $('#<%= commentTxt.ClientID %>').val() + "'}",
                    //async: false,
                    success: function (response) {
                        $('#<%= nameTxt.ClientID %>').val("");
                        $('#<%= commentTxt.ClientID %>').val("");
                        alert("OK");
                    },
                    error: function () {
                        alert("ERROR");
                    }
                });
                e.preventDefault();
            });
        });
    </script>
    

    检查它让我知道反馈Example Source code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 2018-10-03
      • 2017-08-12
      • 1970-01-01
      • 2017-10-06
      相关资源
      最近更新 更多