2

function retrieveHasilRawatJalan()

function retrieveHasilRawatJalan2(row, kd_klp) {

   var hasil_rawat_jalan2;

   var xhttp;
   if (window.XMLHttpRequest) {
     xhttp = new XMLHttpRequest();
   } else {
     // code for IE6, IE5
     xhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }

   xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
     if (xhttp.readyState == 4 && xhttp.status == 200) {
       hasil_rawat_jalan2 = JSON.parse(xhttp.responseText);
       document.getElementById("type").innerHTML = "type of : " + typeof hasil_rawat_jalan2;
       document.getElementById("value").innerHTML = "value of {'1' {'klp_id' : [" + hasil_rawat_jalan2[1]['klp_id'] + "]";
       document.getElementById("size").innerHTML = "object size :[" + parseInt(Object.size(hasil_rawat_jalan2)) + "]";
       document.getElementById("row_start").innerHTML = "start row : " + row;
       document.getElementById("demo").innerHTML = "content : " + hasil_rawat_jalan2;
     }
   }
   xhttp.open("POST", "../lab/get_row_content_from_lab_code/" + kd_klp + "", true);
   xhttp.send();

   //start from here the code is the same.
   var number_of_row = parseInt(Object.size(hasil_rawat_jalan2));

   addNewRow(number_of_row);

 }

variable hasil_rawat_jalan2 contain JSON data and type of this variable is object,

i check type of variable hasil_rawat_jalan2, and call one of the data using this code

document.getElementById("type").innerHTML = "type of : " + typeof hasil_rawat_jalan2;
      document.getElementById("value").innerHTML = "value of {'1' {'klp_id' : [" + hasil_rawat_jalan2[1]['klp_id'] + "]";
      document.getElementById("size").innerHTML = "object size : [" + parseInt(Object.size(hasil_rawat_jalan2)) + "]";
      document.getElementById("row_start").innerHTML = "start row : " + row;
      document.getElementById("demo").innerHTML = "content : " + hasil_rawat_jalan2;

the result is this:

type of : object

value of {'1' {'klp_id' : [HL-004]

object size :[8]

start row : 1

content : [object Object]

the problem is when i put var number_of_row in function addNewRow() it doesnt do anything. but it work if i put number directly like this
addNewRow(8). how to check the error?

4
  • addNewRow definition please. Commented Oct 24, 2015 at 10:39
  • 1
    Why do you parse an integer into an integer? parseInt serves to parse a number from a string. Have you checked the contents of number_of_row at the call site of adNewRow? Commented Oct 24, 2015 at 10:40
  • Why redefine xhttp after the if/else block? The else becomes worthless (well they both do) Commented Oct 24, 2015 at 11:18
  • @pinkfloydx33 hahaha, nice. yes thats my mistake i will delete the one outside the block. thanks Commented Oct 24, 2015 at 15:29

1 Answer 1

3

The function is asynchronous (XMLHttpRequest is, actually). It means that hasil_rawat_jalan2 is not yet defined when you try to use it, because AJAX request doesn't pause subsequent program execution. You need to move addNewRow into onreadystatechange callback and call it once response has become available:

function retrieveHasilRawatJalan2(row, kd_klp) {

   var hasil_rawat_jalan2;

   var xhttp;
   if (window.XMLHttpRequest) {
     xhttp = new XMLHttpRequest();
   } else {
     // code for IE6, IE5
     xhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }

   xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
     if (xhttp.readyState == 4 && xhttp.status == 200) {
       hasil_rawat_jalan2 = JSON.parse(xhttp.responseText);
       document.getElementById("type").innerHTML = "type of : " + typeof hasil_rawat_jalan2;
       document.getElementById("value").innerHTML = "value of {'1' {'klp_id' : [" + hasil_rawat_jalan2[1]['klp_id'] + "]";
       document.getElementById("size").innerHTML = "object size :[" + parseInt(Object.size(hasil_rawat_jalan2)) + "]";
       document.getElementById("row_start").innerHTML = "start row : " + row;
       document.getElementById("demo").innerHTML = "content : " + hasil_rawat_jalan2;

       // Use hasil_rawat_jalan2
       var number_of_row = parseInt(Object.size(hasil_rawat_jalan2));
       addNewRow(number_of_row);
     }
   }
   xhttp.open("POST", "../lab/get_row_content_from_lab_code/" + kd_klp + "", true);
   xhttp.send();
 }
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot dfsq^^. I hope all the smart people kind and helpful just like you and other commenters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.