This is my sample JSON response:
[{"id":11137,"name":"Agra"},{"id":11138,"name":"Albizzate"}]
and i need to iterate each array object and print id and name:
$.ajax({
url: '{{ path('ajax_provinces') }}',
type: 'POST',
dataType: 'json',
data: {region_id: this.value},
success: function(provinces) {},
error: function() { alert("error"); },
complete: function(provinces) {
$('select#regions ~ span > img').fadeOut('slow');
$.each(provinces, function(key, val) {
alert(key + ": " + val);
});
}
});
The problem is i’m gettig strange results: function names, function bodies and other internal stuff from jQuery. It seems like it’s iterating through jQuery library functions! Any clue what’s going on?
The problem is that the
completecallback doesn’t get passed the returned data as an argument:(from the docs)
Presumably the weird key/values you’re seeing are the attributes of the
jqXHRobject.You need to handle the returned data in
success, notcomplete. My understanding is thatcompleteis generally used for actions that should happen regardless of whether the AJAX request successfully returned data (e.g. hiding a loading animation).