-1

I made a query to an API using Ajax but I wasn't able to use the results outside of the ajax constructs.

It is always telling me xx is undefined.

The issue is to be able to use the xx variable outside that ajax constructs.

Please, how do I achieve that?

$.ajax({
       type: "GET",
       url: url,
       dataType : "json",
       success: function(data){


        var xx = (data.results[0].formatted_address);
   }
})
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
Jack Frank
  • 7
  • 1
  • 3
  • 1
    you can't access var xx outside of success function callback - why do you have loop in the title and for-loop in the tags when your code has absolutley no loop? – Jaromanda X May 10 '19 at 05:17
  • Look here : https://stackoverflow.com/questions/5935968/use-variable-outside-the-success-function-from-an-ajax-jquery-call – Gary Houbre May 10 '19 at 05:17

2 Answers2

0

try this one

$.ajax({
       url: url,
       method:"Get",
       async:true,
       dataType : "json"
       }).done(function(data)){
        var xx = (data.results[0].formatted_address);
   }
});
M.Hemant
  • 2,345
  • 1
  • 9
  • 14
Ramesh
  • 1
  • 6
0

Define the variable xx outside the ajax block.

raj240
  • 646
  • 7
  • 17