It's an asynchronous call, so you can't return from it like that.
You'll have to move the code that does something with data
into the callback function (function(data){}
).
function getAreas(){
$.post("/custom_html/weathermap.php",'',
function(data){
//do something with data here, such as calling another function
}, "json");
}
It takes a while to get your head into the asynchronous way of thinking, but you'll work it out. Basically, the bit of code that sends the request is done once the request is sent. That thread of execution will finish, and your browser will just sit there, not doing anything. then the $.post
call will get the data back from weathermap.php
, and your callback function will be called. That starts a whole new thread of execution. Try to think of them as two completely separate executions, one pre-ajax call and one post-ajax call.
Here's some ascii goodness:
V
|
User clicks button
(or something else happens)
|
|
Your JavaScript runs
|
|
And eventually gets
to the ajax call -------> SERVER ------> Server sends data back
|
|
And your callback is run
on the data and execution
continues from here
|
|
V