0

Here I have a javascript code which is giving me wrong output

var myids = "234,235,267";
var output = "";
var myTestValues = "";
var idsArray = myids.split(",");
var xhttp = new XMLHttpRequest();
for(var i=0; i<idsArray.length; i++) {
xhttp.open('GET', 'https://www.example.com/v1/buck?sessionId=123abc456&tid=1&eid=0&id='+idsArray[i]+'', true);
xhttp.timeout = 20000;

xhttp.onload = function () {
    var status = xhttp.status;
    if (status === 200) {
        var data = JSON.parse(xhttp.responseText);
            if (data.aExp.length > 0) {
                  myTestValues += data.aExp[0].id + "." + data.aExp[0].value + "|";
        }
    }
    };

    xhttp.ontimeout = function (e) {
           alert('timeout');
     };
}
xhttp.send();
 output= myTestValues;

expected output : 234.1|235.0|267.3|

Output i'm getting: 267.3| (The last iterated value)

Can somebody please point out where am doing wrong ?

Nilesh Khisadiya
  • 1,560
  • 2
  • 15
  • 27
Rocker rock
  • 155
  • 2
  • 11

0 Answers0