views:

45

answers:

3

As the title may suggest, I have been using jQuery AJAX to try and pull a list of Cities from a database when a Province is selected.

I am using the following code:

$('#province').change(function()
{
    var province = $(this).val();

    /*$.get("<?php echo site_url('cottage/cities'); ?>?province="+province, function(data) 
    {
        console.log(data);
        for (i=0;i<=data.length;i++)
        {
            //$('#citydiv').append(data['city']+'<br/>');
            //$('#city').append('<option value="'+data[i]['city']+'">'+data[i]['city']+'</option>');
        }
    }); */
    $.ajax({
    url: "<?php echo site_url('cottage/cities'); ?>?province="+province,
    method: 'GET',
    dataType: 'json',
    success: onDataReceived
    }); 
    function onDataReceived(series) 
{
    console.log(series);

}   
});

And I also have a Province and City drop down associated with them. The problem is that I keep getting "undefined" returned, as it doesn't like the way my data is sent.

The data looks like:

[{"city_id":"1107","city":"Young's Point","province":"Ontario","lat":"44.490345","lon":"-78.236008"},{"city_id":"1108","city":"Zurich","province":"Ontario","lat":"43.421185","lon":"-81.624832"}]

Any help would be greatly appreciated!

+2  A: 

json encoded strings look like this

{"a":1,"b":2,"c":3,"d":4,"e":5}

whereas you have square brackets try encoding your data in php with json_encode($array);

Christian Smorra
Sorry, I forgot that I cleaned up my results. The raw results had the { } around the data, but had one set of [ ] around the entire data set. (Updated Question)
gamerzfuse
have you tried[...]success: function(data){ console.log(data); } ?
Christian Smorra
I just tried that, but I'm getting 'data is not defined'
gamerzfuse
check your error console, if jquery has a problem with parsing it should show you an error. Quote:In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown.also i kinda don't get why you still have squared brackets arround the json, just to be clear: remove all squared brackets from your response, otherwise its not a valid json string unless youve got arrays inside{"an_array":["item1","item2 ","item3","item4"],"normal_var":"value"} as you can see there are no squared brackets in front or after the json
Christian Smorra
I've been testing it by sending this data: {[{"city_id":"1109","city":"Alonsa","province":"Manitoba","lat":"50.801178","lon":"-98.977608"}]} - It returns: City > 0 > City_id, City, etc
gamerzfuse
ok now ive tested your initial version locally and it works perfectly fine for me...last clues ive got:try inserting this at the top of your php script: header('Content-Type:application/json');or maybe try encoding your json as utf-8:echo utf8_encode(json_encode($array));
Christian Smorra
so what was the problem? :P
Christian Smorra
+1  A: 

Your data should look like

[
    {"city_id":1, "city":"Aberfoyle", "province":"Ontario", "lat":43.472996, "lon":-80.152603},
    {"city_id":2, "city":"Actinolite", "province":"Ontario", "lat":44.543221, "lon":-77.325813}
]

Don't put quotes around numbers, use brackets etc. json_encode is useful (as Christian Smorra stated).

MvanGeest
I updated my data to show what it looks like. I am using json_encode for this.
gamerzfuse
Did you check for the lack of a response using Firebug?
MvanGeest
It finds a response, it just returns UNDEFINED for every record in the database.
gamerzfuse
A: 

Try properly encoding your json response with www.php.net/json_encode

In your onDataReceived() function perform a check on this also to make sure it's valid data.

Even something simple like this would to to stop any JS errors from occuring.

function onDataReceived(series) {
    if(series.length  > 0) {
         // do your stuff
    }
}
Paul Dragoonis