locked
ajax send NOT WORKING RRS feed

  • Question

  • User181930479 posted

    im trying to fetch data from database , but its not working and in the console im not getting any errorss... below is my code

    <input type="text" id="t1" />

    <br />
    <input type="button" value="search" onclick="search1()" />

    <br />

    <p id="demo"></p>


    </div>



    </form>

    <script type="text/javascript">


    function search1()


    {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById("demo").innerHTML = this.responseText;
    }
    };


    xhttp.open("GET", "insert.aspx?opr=search&nm1="+document.getElementById('t1').value,true);
    xhttp.send(null);

    // document.getElementById("d1").innerHTML = xmlhttp.responseText;


    }

    </script>

    and in my insert.aspx.cs code 

    SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=xxxx;Integrated Security=True;Pooling=False");

    string name;
    string gender;
    string opr;

    if (opr == "search") {



    name = Request.QueryString["nm1"].ToString();
    conn.Open();
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT * FROM tbl_thesaurus where Word like (N'"+name.ToString()+"%')";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);


    foreach (DataRow dr in dt.Rows)
    {



    Response.Write(dr["Word"].ToString()); Response.Write("</br>");
    Response.Write(dr["meaning"].ToString()); Response.Write("</br>");
    Response.Write(dr["ressource"].ToString()); Response.Write("</br>");


    }

    conn.Close();

    }

    Monday, January 15, 2018 10:06 AM

All replies

  • User632428103 posted

    Hello naf,

    replace your code in page load this : 

    if (opr == "search") {

    by this :

    if(Request.QueryString["opr"] == "search")

    off course in pageLoad , we initialize all variable and the xmlHttpRequest send reload the page ..every time your variable search will be empty !

    More, you call the same page with a parameter don't forget to find the parameter with a request querystring ..

    Monday, January 15, 2018 2:02 PM
  • User-1838255255 posted

    Hi NAF,

    According to your code and issue, i make a test and modify based on your code, please check the following code: 

    <body>
        <form id="form1" runat="server">
            <div>
                <input type="text" id="t1" />
                <br />
                <input type="button" value="search" onclick="search1()" />
                <br />
                <p id="demo"></p>
    
                <script type="text/javascript">
                    function search1() {
                        debugger;
    
                        var xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("demo").innerHTML = this.responseText;
                            }
                        };
                        var data = document.getElementById("t1").value;
                        var url = "insert.aspx?opr=search&nm1=" + data;
                        xhttp.open("GET", url, true);
                        xhttp.send(null);
                    }
                </script>
    
            </div>
        </form>
    </body>
    
    insert.aspx.cs:
    
       string name;
                string gender;
                string opr = Request.QueryString["opr"];
    
                if (opr == "search")
                {
    //execute your code
                }

    if still not work, please debugger code if meet the error message in Browser-F12-debugger, please post it. 

    Also I provide a alternative solution for your needs:

    https://www.w3schools.com/jquery/ajax_ajax.asp 

    Best Regards,

    Eric Du 

    Tuesday, January 16, 2018 9:58 AM