0

I don't know what is the problem..

        <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>

In my code behind

    [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();

        }
    }

Where is my problem? I can't find it. I'm working with a master page.. is the same that a web form or not? this code is in the master page.. if put a breakpoint in my code behind (SendComment method), the web doesn't stop. it's like that never arrive on it.

Mauro Petrini
  • 341
  • 4
  • 19
  • Are you sure about the URL? http://stackoverflow.com/questions/348689/jquery-ajax-with-asp-net-webmethod-returning-entire-page – Dilish Jan 06 '14 at 03:46
  • @Dilish hi, mm the problem is that i used a master page and the method SendComment is in code behind of the master page.. how i do to call this method from webforms? – Mauro Petrini Jan 06 '14 at 04:08
  • can't you move the `SendComment` webmethod to the page 'PageOne.aspx' itself? also, you can use $('#<%= controID.ClientID %>') where controID is the id of the element, to get a control through jquery, if you're using a masterpage. – Ravimallya Jan 06 '14 at 06:14
  • I don't think it will work. See here http://stackoverflow.com/questions/8577539/using-web-methods-with-master-pages – Dilish Jan 07 '14 at 01:11

2 Answers2

0

Try this may this help you solve your problem

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");
                }
            });
Kamran Khan
  • 1,042
  • 10
  • 21
0

i have attached the sample code for your reference..

  <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>

check it let me know the feedback Example Source code

Prasad Raja
  • 685
  • 1
  • 9
  • 37