How do I save the result of querying this GET in a variable? [closed]

0

I have this code that goes in the VTEX Master Data (bank) and makes an inquiry in a certain email. If you have this email, you have 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 it does not appear 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}}') <
    
asked by anonymous 22.05.2017 / 16:24

1 answer

1

You are saving as an object, so when you give an alert it appears object object , if you give a console.log (return_ajax) you will see the whole object and how to access it. Here is 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><divid="f">f</div>
    
22.05.2017 / 16:52