Function to get text value from a accessed URL

Asked

Viewed 39 times

0

I tried to create a small function:

$(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 like for example: 79.90

I need it that way, using the arrayAmount[0]=, because it is part of another function.

How should I adjust the script?

  • If it is to return a simple text as you said, pq then is passing parameters in the url, id and periodicity?

  • These parameters are to return the correct plan value, each plane has a different ID. The return is only text.

  • If you give a console.log(data); you return what?

  • @sam... Yeah, I noticed he always returns Undefined, not the value.

  • Hmm, I don’t really know how it should be done... if you can show me some tutorial where I can better inform myself I appreciate.

  • I updated the question with the rest of the function.

  • This url you search for your plain text is an API?

  • The bid is to use the variable inside Ajax: https://jsfiddle.net/Lsf8d5u4/2/

  • @sam... The problem is that there are several arrayAmount: arrayAmount[0],arrayAmount[1],arrayAmount[2] and each with different parameters in the URL, so there would be no way I could put directly in the function that prints the value in HTML.

  • @Pedropaulo... yes

  • But then you play as a function parameter whatever is different. Well at first this is it, Ajax will fetch on another page the information, and this can take from fractions of seconds to a few seconds, so you have to build the code that fits into this.

  • @sam... I did a test using your tip, continued giving Undefined.

  • The data only has value inside the Ajax callback. Outside is Undefined.

Show 8 more comments

1 answer

0

Follow an idea of how I would 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 after you have the server response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.