Reputation: 5411
I have the following json response:
[{"id":16,"name":"Madrid","country_id":2,"created_at":"2015-03-30T14:46:13.917Z","updated_at":"2015-03-30T14:46:13.917Z"},{"id":17,"name":"Barcelona","country_id":2,"created_at":"2015-03-30T14:46:13.920Z","updated_at":"2015-03-30T14:46:13.920Z"},{"id":18,"name":"Valencia","country_id":2,"created_at":"2015-03-30T14:46:13.923Z","updated_at":"2015-03-30T14:46:13.923Z"},{"id":19,"name":"Sevilla","country_id":2,"created_at":"2015-03-30T14:46:13.925Z","updated_at":"2015-03-30T14:46:13.925Z"},{"id":20,"name":"Zaragoza","country_id":2,"created_at":"2015-03-30T14:46:13.928Z","updated_at":"2015-03-30T14:46:13.928Z"},{"id":21,"name":"Málaga","country_id":2,"created_at":"2015-03-30T14:46:13.931Z","updated_at":"2015-03-30T14:46:13.931Z"},{"id":22,"name":"Las Palmas de Gran Canaria","country_id":2,"created_at":"2015-03-30T14:46:13.934Z","updated_at":"2015-03-30T14:46:13.934Z"},{"id":23,"name":"Bilbao","country_id":2,"created_at":"2015-03-30T14:46:13.937Z","updated_at":"2015-03-30T14:46:13.937Z"},{"id":24,"name":"Murcia","country_id":2,"created_at":"2015-03-30T14:46:13.940Z","updated_at":"2015-03-30T14:46:13.940Z"},{"id":25,"name":"Valladolid","country_id":2,"created_at":"2015-03-30T14:46:13.942Z","updated_at":"2015-03-30T14:46:13.942Z"},{"id":26,"name":"Córdoba","country_id":2,"created_at":"2015-03-30T14:46:13.945Z","updated_at":"2015-03-30T14:46:13.945Z"},{"id":27,"name":"Palma de Mallorca","country_id":2,"created_at":"2015-03-30T14:46:13.948Z","updated_at":"2015-03-30T14:46:13.948Z"},{"id":28,"name":"Vigo","country_id":2,"created_at":"2015-03-30T14:46:13.951Z","updated_at":"2015-03-30T14:46:13.951Z"},{"id":29,"name":"Alicante","country_id":2,"created_at":"2015-03-30T14:46:13.954Z","updated_at":"2015-03-30T14:46:13.954Z"},{"id":30,"name":"Gijón","country_id":2,"created_at":"2015-03-30T14:46:13.957Z","updated_at":"2015-03-30T14:46:13.957Z"},{"id":31,"name":"Hospital et de Llobregat","country_id":2,"created_at":"2015-03-30T14:46:13.960Z","updated_at":"2015-03-30T14:46:13.960Z"},{"id":32,"name":"La Coruña","country_id":2,"created_at":"2015-03-30T14:46:13.962Z","updated_at":"2015-03-30T14:46:13.962Z"},{"id":33,"name":"Vitoria-Gasteiz\t","country_id":2,"created_at":"2015-03-30T14:46:13.966Z","updated_at":"2015-03-30T14:46:13.966Z"},{"id":34,"name":"Badalona","country_id":2,"created_at":"2015-03-30T14:46:13.969Z","updated_at":"2015-03-30T14:46:13.969Z"},{"id":35,"name":"Santa Cruz de Tenerife","country_id":2,"created_at":"2015-03-30T14:46:13.971Z","updated_at":"2015-03-30T14:46:13.971Z"},{"id":36,"name":"Oviedo","country_id":2,"created_at":"2015-03-30T14:46:13.974Z","updated_at":"2015-03-30T14:46:13.974Z"},{"id":37,"name":"Móstoles","country_id":2,"created_at":"2015-03-30T14:46:13.977Z","updated_at":"2015-03-30T14:46:13.977Z"},{"id":38,"name":"Elche","country_id":2,"created_at":"2015-03-30T14:46:13.981Z","updated_at":"2015-03-30T14:46:13.981Z"},{"id":39,"name":"Sabadell","country_id":2,"created_at":"2015-03-30T14:46:13.985Z","updated_at":"2015-03-30T14:46:13.985Z"},{"id":40,"name":"Santander","country_id":2,"created_at":"2015-03-30T14:46:13.988Z","updated_at":"2015-03-30T14:46:13.988Z"},{"id":41,"name":"Jérez de la Frontera","country_id":2,"created_at":"2015-03-30T14:46:13.991Z","updated_at":"2015-03-30T14:46:13.991Z"},{"id":42,"name":"San Sebastián","country_id":2,"created_at":"2015-03-30T14:46:13.994Z","updated_at":"2015-03-30T14:46:13.994Z"},{"id":43,"name":"Leganés","country_id":2,"created_at":"2015-03-30T14:46:13.997Z","updated_at":"2015-03-30T14:46:13.997Z"},{"id":44,"name":"Almería","country_id":2,"created_at":"2015-03-30T14:46:14.000Z","updated_at":"2015-03-30T14:46:14.000Z"},{"id":45,"name":"Cartagena","country_id":2,"created_at":"2015-03-30T14:46:14.003Z","updated_at":"2015-03-30T14:46:14.003Z"},{"id":46,"name":"Tarrasa","country_id":2,"created_at":"2015-03-30T14:46:14.006Z","updated_at":"2015-03-30T14:46:14.006Z"},{"id":47,"name":"Alcalá de Henares","country_id":2,"created_at":"2015-03-30T14:46:14.008Z","updated_at":"2015-03-30T14:46:14.008Z"},{"id":48,"name":"Fuenlabrada","country_id":2,"created_at":"2015-03-30T14:46:14.012Z","updated_at":"2015-03-30T14:46:14.012Z"},{"id":49,"name":"Pamplona","country_id":2,"created_at":"2015-03-30T14:46:14.015Z","updated_at":"2015-03-30T14:46:14.015Z"},{"id":50,"name":"Burgos","country_id":2,"created_at":"2015-03-30T14:46:14.018Z","updated_at":"2015-03-30T14:46:14.018Z"},{"id":51,"name":"Salamanca","country_id":2,"created_at":"2015-03-30T14:46:14.021Z","updated_at":"2015-03-30T14:46:14.021Z"},{"id":52,"name":"Albacete","country_id":2,"created_at":"2015-03-30T14:46:14.024Z","updated_at":"2015-03-30T14:46:14.024Z"},{"id":53,"name":"León","country_id":2,"created_at":"2015-03-30T14:46:14.027Z","updated_at":"2015-03-30T14:46:14.027Z"},{"id":54,"name":"Getafe","country_id":2,"created_at":"2015-03-30T14:46:14.030Z","updated_at":"2015-03-30T14:46:14.030Z"},{"id":55,"name":"Alcorcón","country_id":2,"created_at":"2015-03-30T14:46:14.033Z","updated_at":"2015-03-30T14:46:14.033Z"},{"id":56,"name":"Cádiz","country_id":2,"created_at":"2015-03-30T14:46:14.035Z","updated_at":"2015-03-30T14:46:14.035Z"},{"id":57,"name":"Huelva","country_id":2,"created_at":"2015-03-30T14:46:14.038Z","updated_at":"2015-03-30T14:46:14.038Z"},{"id":58,"name":"Castellón de la Plana","country_id":2,"created_at":"2015-03-30T14:46:14.041Z","updated_at":"2015-03-30T14:46:14.041Z"},{"id":59,"name":"Badajoz","country_id":2,"created_at":"2015-03-30T14:46:14.044Z","updated_at":"2015-03-30T14:46:14.044Z"},{"id":60,"name":"Logroño","country_id":2,"created_at":"2015-03-30T14:46:14.047Z","updated_at":"2015-03-30T14:46:14.047Z"},{"id":61,"name":"Santa Coloma de Gramanet","country_id":2,"created_at":"2015-03-30T14:46:14.050Z","updated_at":"2015-03-30T14:46:14.050Z"},{"id":62,"name":"La Laguna","country_id":2,"created_at":"2015-03-30T14:46:14.053Z","updated_at":"2015-03-30T14:46:14.053Z"},{"id":63,"name":"Lleida\t","country_id":2,"created_at":"2015-03-30T14:46:14.055Z","updated_at":"2015-03-30T14:46:14.055Z"},{"id":64,"name":"Tarragona","country_id":2,"created_at":"2015-03-30T14:46:14.058Z","updated_at":"2015-03-30T14:46:14.058Z"}]
This is the code on my controller to get this data:
def find_cities
@cities = City.where(country_id: params[:country_id]).all
respond_to do |format|
format.json { render json: {cities: @cities}, success: 200 }
end
end
But I can't get the data I tried:
success: function(data){
for( var i=0; i<data.length; i++ ){
alert( data[i].photo.photo_file_size );
}
}
success: function(data){
var cities = $.parseJSON(data);
}
success: function(data) {
console.log(data.lenght);
},
etc etc etc, but I always get:
undefined
or
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
Thanks for your help.
UPDATE IMAGE NETWORK INSPECTOR
Upvotes: 0
Views: 82
Reputation: 674
please using dataType:'json' in your ajax code:
$.ajax({
url:"save.php",
dataType: "json",
success:function(data) {
handleData(data);
}
});
Upvotes: 1