[Solved] jQuery AJAX code is not working in ASP.Net

Last Reply 8 days ago By pandeyism

Posted 8 days ago

code not executing the ajax part.

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
             $(document).ready(function () {
            $("#ddl1").change(function () {
                var total = $("#ddl1").val();
                var rawhtml = "";
                var textarray = "";
                for (var i = 1; i <= total; i++) {
                    rawhtml += "<textarea id='txtbox" + i + "' class = 'txtoption' wrap = 'hard'/><br/>";
                }
                $("#alltext").html(rawhtml);
            });
            $("#btnsave").click(function () {
                var textlen = $(".txtoption").length;
                alert(textlen);

                for (var i = 1; i <= textlen; i++) {
                    var questions = $("#txtbox" + i).val();
                    alert(questions);
                    $.ajax({
                        message: "hello",
                        url: "feedback_form.aspx/insertDynamicData",
                        type: 'post',
                        
                        data: questions,
                        contentType: "application/json; charset=utf-8",
                        dataType: JSON,
                        success: function (data) {
                            alert("page submitted successfully");
                        },
                        error: function (err) {
                            alert(err);
                        }

                    });
                }
            });

        });
    </script>

 

<body>
    <form id="form1" runat="server">
    Number of questions:
    <select id="ddl1">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <div id="alltext">
    </div>
    <br />
    <input type ="button"  ID="btnsave" runat="server" value="save" />
    <input type="button" value="show data" id="btnShowData" />
    </form>
</body>

 

public partial class feedback_form : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    public static string insertDynamicData(string questions)
    {
        string cmg = ConfigurationManager.ConnectionStrings["conCRM"].ConnectionString;
        using (SqlConnection cnn = new SqlConnection(cmg))
        {
            cnn.Open();
            string query = "insert into dbo.question(questions) values(@questions)";
            SqlCommand cmd = new SqlCommand(query, cnn);
            cmd.ExecuteNonQuery();
            cnn.Close();

        }
        return "Submit";
    }   
}
Posted 8 days ago
himani895 says:
 data: questions,

 There is problem in your code, in data you are passing wrong parameter,  So please make sure you are passing correct parameter.

Please refer below article link for your reference-

Make AJAX Call to ASP.Net Server Side Web service method using jQuery


Posted 8 days ago

Hi himani895,

Please check below modified code.

HTML

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#ddl1").change(function () {
            var total = $("#ddl1").val();
            var rawhtml = "";
            var textarray = "";
            for (var i = 1; i <= total; i++) {
                rawhtml += "<textarea id='txtbox" + i + "' class = 'txtoption' wrap = 'hard'/><br/>";
            }
            $("#alltext").html(rawhtml);
        });
        $("#btnsave").click(function () {
            var textlen = $(".txtoption").length;
            alert(textlen);
            for (var i = 1; i <= textlen; i++) {
                var questions = $("#txtbox" + i).val();
                alert(questions);
                $.ajax({
                    url: "feedback_form.aspx.aspx/insertDynamicData",
                    data: '{questions:"' + questions + '"}',
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        alert("page submitted successfully");
                    },
                    error: function (err) {
                        alert(err);
                    }
                });
            }
        });
    });
</script>
<div>
    Number of questions:
    <select id="ddl1">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <div id="alltext">
    </div>
    <br />
    <input type="button" id="btnsave" runat="server" value="save" />
    <input type="button" value="show data" id="btnShowData" />
</div>

Namespaces

C#

using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;

Code

C#

[WebMethod]
public static string insertDynamicData(string questions)
{
    string cmg = ConfigurationManager.ConnectionStrings["conCRM"].ConnectionString;
    using (SqlConnection cnn = new SqlConnection(cmg))
    {
        cnn.Open();
        string query = "insert into dbo.question(questions) values(@questions)";
        SqlCommand cmd = new SqlCommand(query, cnn);
        cmd.ExecuteNonQuery();
        cnn.Close();
    }

    return "Submit";
}