Welcome to WebmasterWorld Guest from 54.145.167.92

Forum Moderators: open

Message Too Old, No Replies

json headache

cant get my json to return properly

   
1:31 pm on Feb 20, 2012 (gmt 0)

10+ Year Member



this is weird!
i have a little function that grabs a list of postcodes within a certain geographical area:

function getPostcodes(north, south, west, east)
{
postcodes = $.getJSON('/properties/search/get-postcodes-in-area/north/'+north+'/south/'+south+'/west/'+west+'/east/'+east, function(data)
{
postcodes = data;
//console.log(postcodes);
return postcodes;
});
console.log(postcodes);
return postcodes;
}


as you can see i added two console.log() commands.
the first one(that's commented out) logs this:

["EC1", "EC1A", "EC1M", "EC1N", "EC1R", "EC1V", "EC1Y", "EC2", "EC2A", "EC2M", "EC2N", "EC2R", "EC2V", "EC2Y", "EC3", "EC3A", "EC3M", "EC3N", "EC3P", "EC3R", "EC3V", "EC4", "EC4A", "EC4M", "EC4N", "EC4R", "EC4V", "EC4Y", "NW1", "SW1A", "SW1E", "SW1H", "SW1P", "SW1V", "SW1W", "SW1X", "SW1Y", "W1", "W1B", "W1C", "W1D", "W1F", "W1G", "W1H", "W1J", "W1K", "W1M", "W1S", "W1T", "W1U", "W1W", "WC1", "WC1A", "WC1B", "WC1E", "WC1H", "WC1N", "WC1R", "WC1V", "WC1X", "WC2", "WC2A", "WC2B", "WC2E", "WC2H", "WC2N", "WC2R"]

which is what i need! but the second console log, which i expected to be the same, returns a resoponse object, with status equaling 200 and responseText equaling my json, and a whole bunch of other stuff.

yet console logging postcodes.responseText is undefined!

what the hell am i missing here lol? how do i return my json?
2:34 pm on Feb 20, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



You're making an AJAX call... the first "A" in AJAX stands for Asynchronous. The anonymous function that you're passing as the last parameter to getJSON is the callback method that will be called at some asynchronous point in the future. So the order of operations in your code might go something like this:


postcodes = $.getJSON(...);
// Asynchronous request send to the server, but no response yet
// postcodes equals nothing useful
console.log(postcodes);
// postcodes still equals nothing useful
return postcodes;
// postcodes still equals nothing useful
// Response comes back from server and anonymous callback function executes


Whatever it is that you need to do with the postcodes returned from the server, you need to put that logic in the callback function. You can't "return" the value because there's nothing to return it to since the request was asynchronous.

Hope that helps.
3:05 pm on Feb 20, 2012 (gmt 0)

10+ Year Member



oh yeah. lol!

well in that case the question just changed :-s

here's my refined code so far

$('#find').click(function(){
bounds = google.maps.Polygon.prototype.getBounds();
north = bounds.getNorthEast().lng();
south = bounds.getSouthWest().lng();
west = bounds.getSouthWest().lat();
east = bounds.getNorthEast().lat();
//console.log(north);
//console.log(south);
//console.log(west);
//console.log(east);

$.getJSON('/properties/search/get-postcodes-in-area/north/'+north+'/south/'+south+'/west/'+west+'/east/'+east, function(data){
if(!data)
{
north = north + 0.00002;
south = south - 0.00002;
west = west - 0.00002;
east = east + 0.00002;
//need to run get json again!
}
});
});



essentially if no data is returned, i make the map bounds a little larger, and i need to try again until we get results, but how can i call it again from inside itself? and if i do, each point is set at the start of the click function :-s
3:33 pm on Feb 20, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



In this case, I would simply not use an anonymous function. Instead, give your function a name and call it like so:


$('#find').click(function(){
bounds = google.maps.Polygon.prototype.getBounds();
north = bounds.getNorthEast().lng();
south = bounds.getSouthWest().lng();
west = bounds.getSouthWest().lat();
east = bounds.getNorthEast().lat();
//console.log(north);
//console.log(south);
//console.log(west);
//console.log(east);
function getPostCodes(data){
if(!data) {
north = north + 0.00002;
south = south - 0.00002;
west = west - 0.00002;
east = east + 0.00002;
//need to run get json again!
$.getJSON('/properties/search/get-postcodes-in-area/north/'+north+'/south/'+south+'/west/'+west+'/east/'+east, getPostCodes);
return;
}
// If you make it here, you've got your result!
}
$.getJSON('/properties/search/get-postcodes-in-area/north/'+north+'/south/'+south+'/west/'+west+'/east/'+east, getPostCodes);
});
3:09 am on Feb 21, 2012 (gmt 0)

10+ Year Member



awesome! you sir, are a legend. :-D