up vote 0 down vote favorite
share [fb]

I am trying to use json output in jquery method.

$(function() {
    $.getJSON("/items/list/", function(json) {
        var source = json;
        alert(source.os[0]);
    });
});

It does not work. But when I directly goto the url(/items/list/), I see the json output. It looks something like this..

{"os":["Windows","Chrome","Mac OS X"], "languages":["php", "Java"]}

I appreciate any help.

Thanks.

link|improve this question
1  
What do you mean "it does not work"? – matthewh May 3 at 11:30
1  
Hum , it seems to work for me. It shows "Windows" as it could. – Timon.Z May 3 at 11:31
feedback

2 Answers

Perhaps mime type for json is not set in header before outputting:

Try:

$(function() {
    $.getJSON("/items/list/", function(json) {
        var source = $.parseJSON(json);
        alert(source.os[0]);
    });
});
link|improve this answer
feedback

If you are aware of Firefox Firebug addon that might help you .

Goto the script tab, just keep a breakpoint in the 4th line that is var source = json; and have a look at the value of source in the right side of the firebug.

If the above doesn't help, you can try this jQuery.parseJSON( json ) which converts JSON string and returns JavaScript object.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.