0

Can someone show me ho can i use the variables stored in this Ajax call:

            $.get( "nuoviServiziReview.html", function( data ) {
                var nuovoServizioReview = $('<div/>',{id:'servizio'+ incremento});

                nuovoServizioReview.html(data);
                nuovoServizioReview.appendTo(parentDiv2);  

                servizio = nuovoServizioReview;
                reviewOption1 = nuovoServizioReview.find('.select1');
                reviewOption2 = nuovoServizioReview.find('.select2');
                reviewOption3 = nuovoServizioReview.find('.select3');
                prezzoFisso = nuovoServizioReview.find('.select1').children('label:last-child');

                nuovoIdCheckbox.attr('checked', true);
                $(thisBtnOk).addClass('hidden');

                //Add title to Review Section
                var newServiceTitle = ($(idInputeText).val());
                servizio.text(newServiceTitle);
            }); 

After the call is done, I need to use the variables inside it but from outside the function. I tried to console.log for example the "servizio" var but it print out nothing on the console. I tried to check different examples but I didn't understand how to use them. Can someone show me a practical example? This ajax call is done after a button is clicked on my index page.

Daniele Martini
  • 143
  • 1
  • 1
  • 13
  • Declare variable before your $.get – prashant Apr 13 '16 at 09:11
  • if you want to access that jquery element object then get it from document outside the function.if simple variable you want to access then just make it global or outside the get request. for more clarification you should share a jsfiddle, what do you want to do – DK3 Apr 13 '16 at 09:12
  • I just need to use a variable declared inside thi ajax call from outside the function. i declare the varible servizio outside the function but if i try to console.log it after the function get is done, it print out nothing. – Daniele Martini Apr 13 '16 at 09:20
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – madalinivascu Apr 13 '16 at 09:40

2 Answers2

0

Ajax is triggered asynchronous so the values are not accessible outside the ajax success function ,do your logic there

function( data ) {
//all data related logic
}
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0
 $.ajax({
    url: url,
    type: type,
    dataType: "json",
    data: JSON.stringify(data),
    contentType: "application/json",
    cache: false,
    timeout: 5000,
    complete: function() {
        //called when complete
        console.log('process complete');


    },

    success: function(data) {

        console.log('process sucess');

        action(data);

    },

    error: function(e) {
        console.log('process error');
        console.log(e);

    },
});

Make a function like the action function which takes the data, apply data operations through it.

boomcode
  • 399
  • 1
  • 14