I\'m trying to parse this very long and complicated JSON that foursquare gives me. This is my AJAX request:
$.ajax({
url: \'https://api.foursquare.
You should do:
$.ajax({
// some other code
success: getVenues
});
You are telling ajax: "use getVenues function", not "use getVenus(data) value". As for second question:
var l = data.response.groups.length;
for (var i = 0; i < l; i++) {
var group = data.response.groups[i];
var k = group.items.length;
for (var j = 0; j < k; j++) {
var venue = group.items[j].venue;
// use venue as you wish
}
}
The tutorials you see online are likely declaring the success
callback as an anonymous function. In those cases, data
isn't being passed to a function, it's being declared as the parameter of that function. jQuery is nice enough to handle passing the response from the AJAX call to the success function as the first parameter, whatever you choose to name it (data
just makes the most sense).
Additionally, if you specify dataType: 'json'
on your $.ajax()
call, jQuery will parse the JSON response before passing it to that function, ensuring that it's valid JSON and that you have an object to work with inside the function. If the response isn't valid JSON, the success
callback won't be executed, and instead the error
callback (if you've specified one) will be executed.
In your case, you're passing a function reference, so assuming your getVenuesfunction looks like this:
function getVenues(data) {
// do something
}
then you can simply do:
success: getVenues
in the object you're passing to $.ajax()
.
The success property of the object in your ajax call just needs function name or an function object. You either give it just the name, like that:
$.ajax({
url: 'https://api.foursquare.com/v2/venues/explore',
dataType: 'json',
data: 'limit=7&ll='+latitude+','+longitude+'&client_id='+client_id+'&client_secret='+client_secret+'',
async: false,
success: getVenues
});
or you do this:
$.ajax({
url: 'https://api.foursquare.com/v2/venues/explore',
dataType: 'json',
data: 'limit=7&ll='+latitude+','+longitude+'&client_id='+client_id+'&client_secret='+client_secret+'',
async: false,
success: function(data) { getVenues(data) }
});