locked
xmlhttprequest last variable not SENT RRS feed

  • Question

  • User181930479 posted

    I am sending data to another page and inserting it into the database using xmlhttprequest ,

    but the last variable im sending is getting value = null in the request.query string , any hint ? code is below :

    function insert() {
    
    
               var SN = prompt("Please assign name to your new study", "...");
    
               if (SN != null) {
                 
    
               var xmlhttp = new XMLHttpRequest();
               var anchors = document.getElementById('mySidenav').getElementsByTagName('a');
    
               for (var i = 0; i < anchors.length; i++) {
    
                   var x = anchors[i].text
                   var y = anchors[i].href
                
                
    
                   xmlhttp.open("GET", "addnewstudy.aspx?nm=" + x + "&gd=" + y + "&sn=" + SN +"&", false);
                 
               xmlhttp.send(null);
    
                   }
    
           }
           alert("New Study" + " " + SN + " " +" inserted in database");
    
           document.getElementById("mySidenav").style.width = "0";
    
       }

    code for addnewstudy.aspx:

     string name;
        string gender;
        string SN;
    
        protected void Page_Load(object sender, EventArgs e)
        {
           
                name = Request.QueryString["nm"].ToString();
                gender = Request.QueryString["gd"].ToString();
                SN = Request.QueryString["sn"].ToString();
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into tbl_myresearches (NodeName,NodeLink,StudyName) values(N'" + name.ToString() + "',N'" + gender.ToString() + "',N'"+ SN.ToString() +"')";
               
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
    
           
        }
    }
    Wednesday, December 13, 2017 7:25 AM

Answers

  • User-707554951 posted

    Hi NAF, 

    As far as I know, Synchronous XMLHttpRequest (async = false) is not recommended because the JavaScript will stop executing until the server response is ready. If the server is busy or slow, the application will hang or stop.

    So, set it true and use getAttribute("href") to get href value of a tag.

    https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp

    Besides, I use the following code. it works well, you could refer to it:

     <script type="text/javascript">
            function insert() {
                var SN = prompt("Please assign name to your new study", "...");
                if (SN != null) {
                    var xmlhttp = new XMLHttpRequest();
                    xmlhttp.onreadystatechange = function () {
                        if (this.readyState == 4 && this.status == 200) {
                            document.getElementById("demo").innerHTML = this.responseText;
                        }
                    };
                    var anchors = document.getElementById('mySidenav').getElementsByTagName('a');
                    for (var i = 0; i < anchors.length; i++) {
    
                        var x = anchors[i].text
                        var y = anchors[i].getAttribute("href")
                        var url = "TestGetPara.aspx?nm=" + x + "&gd=" + y + "&sn=" + SN;
                        xmlhttp.open("GET", url, true);
                        xmlhttp.send();
                    }
                    }
                        
                alert("New Study" + " " + SN + " " + " inserted in database");
                document.getElementById("mySidenav").style.width = "0";
            }
    
        </script>
        <div id="mySidenav">
                <a href="href">text</a>
            </div>
            <input type="button" value="insert" onclick="insert()" />
    <div id="demo"> </div>

    In TestGetPara.aspx.cs:

     protected void Page_Load(object sender, EventArgs e)
            {
                string name;
                string gender;
                string SN;
                name = Request.QueryString["nm"].ToString();
                gender = Request.QueryString["gd"].ToString();
                SN = Request.QueryString["sn"].ToString();
                TextBox1.Text = name + "|" + gender + "|" + SN;
            }

    Output:

    Best regards 

    Cathy 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 19, 2017 3:04 AM