Herman Nz February 2016

Loop Calling Function in Native Ajax

Here's my native ajax code:

            function script_grabbed(str) {
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("numvalue").value = xmlhttp.responseText;
                    var result = document.getElementById("numvalue").value;
                    if (typeof result !== 'undefined'){             
                        alert('Data Found:' + result);
                        //start: new request data for #valdata
                        xmlhttp.open("POST", "inc.php?q="+str, true);
                        document.getElementById("valdata").value = xmlhttp.responseText;
                        xmlhttp.send(null);
                        var dataval = document.getElementById("valdata").value;
                        if (typeof dataval !== 'undefined'){    
                            alert('Data Bound:' + dataval);
                            //continue to call maps
                            script_dkill()
                            }
                        //end: new request data for #valdata
                        }
                    }
                }
                xmlhttp.open("POST", "inc_num.php?q="+str, true);
                xmlhttp.send(null);
            }

From the code, let me explain that:
I want to get data/value from result and dataval. After I get the data, I execute script_dkill() function.

However, It creates loop and never get to script_dkill.
So, the question is: How to get to script_dkill and execute it?

For example: The script_dkill() has content as follow:

function script_dkill(){
   alert('Hallo, you call me!');
}

Any help, please...

Answers


Arun P Johny February 2016

You need to use a different XMLHttpRequest object for the second request, since you are using the same object it will call the same onreadystatechange event again and again

function script_grabbed(str) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      document.getElementById("numvalue").value = xmlhttp.responseText;
      var result = document.getElementById("numvalue").value;
      if (typeof result !== 'undefined') {
        alert('Data Found:' + result);
        //start: new request data for #valdata

        var xmlhttp2 = new XMLHttpRequest();
        xmlhttp2.onreadystatechange = function() {
          if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) {
            document.getElementById("valdata").value = xmlhttp2.responseText;
            var dataval = document.getElementById("valdata").value;
            if (typeof dataval !== 'undefined') {
              alert('Data Bound:' + dataval);
              //continue to call maps
              script_dkill()
            }

          }
        }
        xmlhttp2.open("POST", "inc.php?q=" + str, true);
        xmlhttp2.send(null);

        //end: new request data for #valdata
      }
    }
  }
  xmlhttp.open("POST", "inc_num.php?q=" + str, true);
  xmlhttp.send(null);
}

Post Status

Asked in February 2016
Viewed 2,711 times
Voted 6
Answered 1 times

Search




Leave an answer