I am stuck with this problem for an hour. I think this has something to do with variable reach? Anyway, here is the code:
function loadRoutes(from_city) { $.ajax( { url: './ajax/loadRoutes.php', async : true, cache : false, timeout : 10000, type : "POST", dataType: 'json', data : { "from_city" : from_city }, error : function(data) { console.log('error occured when trying to load routes'); }, success : function(data) { console.log('routes loaded successfully.'); $('#upperright').html("");
}
For each section, it works very well inside the success callback area. I see that the Firebug console displays route identifiers without any problems.
To unleash the goal, I believe that it would be better to simply return the data object, which is in JSON format, to a variable in the caller's function, for example:
//ajax load function function findFromCity(continent, x, y) { console.log("clicked on " + continent + ' ' + x + ',' + y); $.ajax( { url: './ajax/findFromCity.php', async : true, cache : false, timeout : 10000, type : "POST", dataType : 'json', data : { "continent" : continent, "x" : x, "y" : y }, error : function(data) { console.log('error occured when trying to find the from city'); }, success : function(data) { var cityname = data.from_city; //only query database if cityname was found if(cityname != 'undefined' && cityname != 'nowhere') { console.log('from city found : ' + cityname); data = loadRoutes(cityname); console.log(data); } } }); }
Then all of a sudden everything stops working! The Firebug console reports the data object as "undefined" ... does the return object assign the loadRoutes (cityname) method?
Sorry that my general knowledge in javascript is pretty limited, so now I'm just a βcopycatβ to work on my code in an amateur way.
Edited: Seeing Nickβs prompt, let me work on it now and see how it happens.
Edited 2:
bear with me still stuck in this:
//ajax load function function findFromCity(continent, x, y) { console.log("clicked on " + continent + ' ' + x + ',' + y); var cityname = "nowhere"; //variable initialized. $.ajax( { url: './ajax/findFromCity.php', async : true, cache : false, timeout : 10000, type : "POST", dataType : 'json', data : { "continent" : continent, "x" : x, "y" : y }, error : function(data) { console.log('error occured when trying to find the from city'); }, success : function(data) { cityname = data.from_city; //only query database if cityname was found if(cityname != 'undefined' && cityname != 'nowhere') { console.log('from city found : ' + cityname); //data = loadRoutes(cityname); //console.log(data); } } }); return cityname; //return after ajax call finished. }
The Firebug console produces something interesting:
nowhere from city found : Sydney
I thought that the order should at least be changed as follows:
from city found : Sydney nowhere
So basically, a variable defined in the success area has a completely different area from the same variable outside? It sounds weird at first, but now I see it.
However, I don't know how to pass the json object from the success callback to assign it to another variable ...
Conclusion: well, I got it while working on a "pass by reference" to use a side effect to change the variable passed by the functional parameter, now ... Which is not directly related to this issue.