2
Hello I am developing a site to train a little js, but I came across the following situation: I have a javascript object that I created to facilitate the Ajax queries, but when I am making the requests sometimes a link is solved or terminated(I don’t know the right technical terminology) before the other one, so this keeps my site sometimes not showing the values I want (I’m using the push array method per hour),then I thought about creating a new Object while making the requests with the name of each item, to take the values more easily and have no problems regarding the speed of the resolution of links, but when I perform my function only returns the value of the last name of the created object.
to better illustrate:
Object:
ExchangeInfo = {
mercadobitcoin:{
name:"mercadobitcoin",
bch:{
orderbook:"https://www.mercadobitcoin.net/api/BCH/orderbook/"
}
},
braziliex:{
name:"braziliex",
bch:{
orderbook:"https://braziliex.com/api/v1/public/orderbook/bch_brl"
}
},
negociecoins:{
name:"negociecoins",
bch:{
orderbook:"https://broker.negociecoins.com.br/api/v3/bchbrl/orderbook"
}
}
};
Function:
v = {};
function getApiLinks() {
for (prop in ExchangeInfo){
var xml = new XMLHttpRequest();
xml.open("GET",ExchangeInfo[prop]["bch"]["orderbook"],true);
xml.send();
xml.onreadystatechange = function(){
if(this.status == 200 && this.readyState == 4){
myObj = JSON.parse(this.responseText);
v[ExchangeInfo[prop]["name"]]=this.responseText;
return v[ExchangeInfo[prop]["name"]] = myObj; // resolução parcial que só retorna o ultimo valor consultado exemplo abaixo.
// return v.push(myObj); resolução atual intermitente
}
}
}
}
returned object:
{
negociecoins:{
ask[
//dados retornados pelo ajax
],
bid:[
//dados retornados pelo ajax
]
}
}
Expected object:
{
mercadobitcoin:{
ask[
//dados retornados pelo ajax
],
bid:[
//dados retornados pelo ajax
]
},
braziliex:{
ask[
//dados retornados pelo ajax
],
bid:[
//dados retornados pelo ajax
]
},
negociecoins:{
ask[
//dados retornados pelo ajax
],
bid:[
//dados retornados pelo ajax
]
}
}
Sorry if the question got too long, I tried to give as much information as possible.
Thanks for the answer, it worked fine, I didn’t know I couldn’t do with asynchronous requests because it was working with the array, but not with object. was even worth reply.
– Rodrigo Gabriel
I just didn’t mark the answer with useful before, because my score was too low.
– Rodrigo Gabriel