0
var path_data = path+"?q=map/get_data/"+param1+'/' +param2+'/'+param3+'/' +param4+'/' +param5;
var path_map_center = path+"?q=map/get_center/"+param1;

$.getJSON(path_map_center, function(data) {
   center_lati = data.lati;
   center_longi = data.longi;
});

// How to ensure ,center_lati center_longi are assigned before I move to next line of code. Basically how to wait till above execute.

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • 1
    You can't. Just put the code that requires the asynchronous operation to complete **inside** that callback function. – Pointy Sep 07 '13 at 15:18
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Bergi Sep 07 '13 at 16:23

1 Answers1

2

In ajax based system it is advicable to follow async developement meaning all code which need to work with data returned by the ajax call must added to the respective callback method

var path_data = path + "?q=map/get_data/" + param1 + '/' + param2 + '/' + param3 + '/' + param4 +'/' + param5;
var path_map_center = path + "?q=map/get_center/" + param1;

$.getJSON(path_map_center, function(data) {
    var center_lati = data.lati;
    var center_longi = data.longi;
    //all code that need to work with center_lati and center_longi should be added here 
});
// do no put any ode that need to work with center_lati and center_longi should be added here 

Still if you are not planning to use the async mode then use the async option

var path_data = path + "?q=map/get_data/" + param1 + '/' + param2 + '/' + param3 + '/' + param4 + '/' + param5;
var path_map_center = path + "?q=map/get_center/" + param1;

$.ajax({
    url: path_map_center,
    dataType: 'json',
    type: 'GET',
    async: false,
    success: function (data) {
        center_lati = data.lati;
        center_longi = data.longi;
    }
})
Barmar
  • 741,623
  • 53
  • 500
  • 612
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531