How do I save the result of this GET query in a variable?

Asked

Viewed 245 times

0

I have this code that goes in the Master Data VTEX (bank) and makes a query in a given email. If you have this email he has to bring the customer’s phone number.

How do I save the result of the object in a variable? It only appears on the console. The way I did not show the result in the code.

function ContactCreateByEmail(storeName, dataEntity, CriteoEmail) {    
    var cl_url = "https://api.vtexcrm.com.br/|||||||||||||||||/dataentities/|||||||||||||||||/search?_fields=homePhone&_where=email={{CriteoEmail}}";

    $.ajax({      
        headers: {        
            "Accept": "application/vnd.vtex.ds.v10+json",
                    "Content-Type": "application/json"      
        },
              type: 'GET',
              url: cl_url,
              success: function(data) {      
            var retorno_ajax = data[0]; //or something similar

            if (retorno_ajax == null) {       
                console.log('nenhum dado retornado');      
            }       
            else {      
                console.log('retorno_ajax');      
            }      
        },

    });  
    __blc['id'] = "61ab36efda06b8c498209f4d0c725948";             
    try {    
        lc.sendData({      
            evento: "sms_transacional",
                  transactionId: "{{transactionId}}",
                  customer_id: "{{CriteoEmail}}",
                     numero: (retorno_ajax),

        });  
    } catch (e) {  } 
}  
ContactCreateByEmail('duloren', 'CL', '{{CriteoEmail}}') <
  • But you’re already saving, look at that line: var retorno_ajax = data[0].

  • You can declare this variable at the beginning of the code javascript, out of function to gain access to other parts of the code

  • Correct, but it only appears on the console... Downstairs I’m calling the variable to appear as a result in the API and it does not appear...and if I give an Alert with the variable it does not show the phone...it appears "Object Object"

  • This variable is declared inside the block success of the call Ajax, therefore cannot be accessed elsewhere. Do as I mentioned above, declare it out of the function ContactCreateByEmail, then you should be able to access it both in the return of Ajax and in another part of your javascript.

  • @Ricardopunctual I did and it didn’t work. Maybe I did it wrong. Please edit and send me?

  • You can put your complete code, and where you are using the variable?

  • @Ricardopunctual this is the complete code...this is a transactional email code that I have to return some values, then I am adding it in the event of finalizing purchase. I am adding via Tag Manager. This one is complete

Show 2 more comments

1 answer

1

You are saving as an object, so when giving an Alert appears object object, if Voce gives a console.log(return ajax) will see the whole object and how to access it. Follow an example:

var resposta;


function go(){
$.ajax({
type: 'GET',
url: 'https://gist.githubusercontent.com/ografael/2037135/raw/5d31e7baaddd0d599b64c3ec04827fc244333447/estados_cidades.json',
dataType: 'json'
}).done(function(done){
resposta = done[0].nome;
})
}

go();

setTimeout(function(){
alert(resposta)
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="f">f</div>

  • Rafael Augusto, I used this example and Alert now brings the phone number... Now how do I play the result of this variable (the number) in the bottom block after this function? There in my code I call but it does not seem.

  • I added it for you to see, but I don’t know what you really want to do, so in case, the top code, wait for an API response time to validate. Anything warns me.

  • Let me explain. I need to return to a company this data filled.She gave me this code to pass the variables with the value. Try ' Lc.sendData(' event: "sms_transactional", transactionId: "{transactionId}}", customer_id: "{{Criteoemail}}", number: (retorno_ajax), }); But the number I didn’t have on the screen, so I had to give that get to know the number of the client. The structure is as in the first question at the moment, but it does not pull the result of the variable.

Browser other questions tagged

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