-1

Fist off, here's the jSON object I created with PHPs json_encode function

{
    "Gatwick":[
        {
            "destination":"VCE",
            "destination_name":"Venezia Marco Polo"
        },{
            "destination":"VCE",
            "destination_name":"Venezia Marco Polo"
        },{
            "destination":"VCE",
            "destination_name":"Venezia Marco Polo"
        }
    ],
    "Heathrow":[
        {
            "destination":"VCE",
            "destination_name":"Venezia Marco Polo"
        },{
            "destination":"VCE",
            "destination_name":"Venezia Marco Polo"
        }
    ]
}

Which I think is ok as I understand it. I requested the object using jQuerys $.getJSON(...) function.

Assuming all of that is correct, I can't for the life of me figure out how to access the data in the json object or even illicit any kind of response to indicate anything is happening under the hood.

My latest attempt was to copy the example from the jQuery docs like this...

$.getJSON(url, callBack);

function callBack(data){
    $.each(data.items, function(i, item){
        alert("YO");
    });
}

Which generates the following javascript error

jquery-1.2.6.min.js (line 19) TypeError: Result of expression 'object' [undefined] is not an object.

Which is a little cryptic. Especially since using this

function callBack(data){ alert(data); }

says [object Object]

but this

function callBack(data){ alert(data[0]); }

gives me nothing.

Where am I going wrong here?

3 Answers 3

3

The ".items" in the jQuery example is a .NET thing - you have data.Gatwick[0].destination == 'VCE'

Sign up to request clarification or add additional context in comments.

Comments

1

you don't have 'items' in your data object... just use

$.each(data, function(i, item){

at which point you can do:

item[0].destination

Comments

0

The JSON that the PHP is returning is not an array. Notice the curly braces, not square braces.

1 Comment

that is true.. but I'm pretty sure jquery's $.each will still work, the i variable will just contain the dict key (or whatever javascript calls it, I'm a python guy)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.