0
How can I simplify and leave everything in a single function the script below?
var plano_0 = new XMLHttpRequest();
plano_0.open('GET', 'buscar_valor.php?id=10', false);
plano_0.send(null);
plano_0=plano_0.responseText;
plano_0_valor=plano_0.replace(".", ",")
arrayAmount[0]=plano_0_valor;
var plano_1 = new XMLHttpRequest();
plano_1 .open('GET', 'buscar_valor.php?id=20', false);
plano_1 .send(null);
plano_1 =plano_0.responseText;
plano_1_valor=plano_0.replace(".", ",")
arrayAmount[1]=plano_1_valor;
var plano_2 = new XMLHttpRequest();
plano_2.open('GET', 'buscar_valor.php?id=30', false);
plano_2.send(null);
plano_2=plano_0.responseText;
plano_2_valor=plano_0.replace(".", ",")
arrayAmount[2]=plano_2_valor;
You could do it for example:
function buscaValor () {
var plano = new XMLHttpRequest();
plano.open('GET', 'buscar_valor.php?id=id_do_array', false);
plano.send(null);
plano=plano.responseText;
valor=plano.replace(".", ",")
}
arrayAmount[0]=valor_id10;
arrayAmount[1]=valor_id20;
arrayAmount[2]=valor_id30;
Obviously the above function is wrong, but it’s just an example to show how it could be.
When using
false
in theplano.open
you are saying that Ajax should be synchronous, IE, the code will only continue to be executed after a request reply. This is an unwelcome practice. Ajax must asynchronous, as is its nature.– Sam