Function to get text value of an accessed URL

0

I tried to create a small function, it looked like this:

$(document).ready(function(){
            var value0;
            $.get( "file.php?id=1", function(data){
                value0 = data;
            });
            arrayAmount[0]=value0;

            var value1;
            $.get( "file.php?id=2", function(data){
                value1 = data;
            });
            arrayAmount[1]=value1;

            var value2;
            $.get( "file.php?id=3", function(data){
                value2 = data;
            });
            arrayAmount[2]=value2;
        }

        function buyVps(){
            var vpsDetails='Processor : '+arrayProcessor[sliderValue]+' GHZ'+'\nRAM : '+arrayRam[sliderValue]+' MB'+'\nRAID Storage : '+arrayStorage[sliderValue]+' GB'+'\nMySql Databases : '+arrayMySqlDB[sliderValue]+' GB'+'\nMonthly Price : '+'R$ '+arrayAmount[sliderValue];window.open(arrayLink[arrayBlocks[sliderValue]], '_blank');
        };

I need it to take the value that is returned when the URL is accessed. It is not in JSON, it returns in plain text, only a value such as eg: 79.90

I need it to be this way, using arrayAmount[0]= , because it is part of another function.

How do I adjust the script?

    
asked by anonymous 03.09.2018 / 15:07

1 answer

0

Here's an idea of what I'd do:

var arrayAmount = Array();

$(document).ready(function(){
    // quando o documento estiver ok, buscar os dados no servidor
    buscarNoServidor();
}

function buscarNoServidor(){
    $.get( "http://localhost/2018/buscar_valor.php?id=48&periodicidade=monthly", function(data){
        arrayAmount[0] = data;

        // ao receber os dados, chamar as demais funções
        buyVps();
    });
}

// só chamar esta função, quando tiver resposta do servidor
function buyVps(){
    var vpsDetails='Processor : '+arrayProcessor[sliderValue]+' GHZ'+'\nRAM : '+arrayRam[sliderValue]+' MB'+'\nRAID Storage : '+arrayStorage[sliderValue]+' GB'+'\nMySql Databases : '+arrayMySqlDB[sliderValue]+' GB'+'\nMonthly Price : '+'R$ '+arrayAmount[sliderValue];window.open(arrayLink[arrayBlocks[sliderValue]], '_blank');
};

Obviously you will adapt to your need, but the idea is that you can only use the value once you have the server response.

    
03.09.2018 / 16:08